From 7bdeccd30b1f9e4f593604946fd6ed0ee001a5b2 Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 6 Aug 2024 12:44:36 +0200 Subject: [PATCH 1/8] Update UserDefaultChecker.php Add another layer of in memory cache. Avoids pinging the "real" cache for the same request. --- src/Checkers/User/UserDefaultChecker.php | 36 +++++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Checkers/User/UserDefaultChecker.php b/src/Checkers/User/UserDefaultChecker.php index 28c94106..f73244e2 100644 --- a/src/Checkers/User/UserDefaultChecker.php +++ b/src/Checkers/User/UserDefaultChecker.php @@ -152,13 +152,17 @@ protected function userCachedRoles(): array { $cacheKey = 'laratrust_roles_for_'.$this->userModelCacheKey().'_'.$this->user->getKey(); - if (! Config::get('laratrust.cache.enabled')) { - return $this->user->roles()->get()->toArray(); - } - - return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { - return $this->user->roles()->get()->toArray(); - }); + return Cache::store('array')->rememberForever( + $cacheKey, + function() { + if (! Config::get('laratrust.cache.enabled')) { + return $this->user->roles()->get()->toArray(); + } + + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { + return $this->user->roles()->get()->toArray(); + }); + }); } /** @@ -170,13 +174,17 @@ public function userCachedPermissions(): array { $cacheKey = 'laratrust_permissions_for_'.$this->userModelCacheKey().'_'.$this->user->getKey(); - if (! Config::get('laratrust.cache.enabled')) { - return $this->user->permissions()->get()->toArray(); - } - - return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { - return $this->user->permissions()->get()->toArray(); - }); + return Cache::store('array')->rememberForever( + $cacheKey, + function() { + if (! Config::get('laratrust.cache.enabled')) { + return $this->user->permissions()->get()->toArray(); + } + + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { + return $this->user->permissions()->get()->toArray(); + }); + }); } /** From 1464e9c4511918a7249db76c25155371daac676d Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 6 Aug 2024 12:47:53 +0200 Subject: [PATCH 2/8] Update RoleDefaultChecker.php Same as with UserDefaultChecker, it avoids pinging the real cache multiple times for the same request. --- src/Checkers/Role/RoleDefaultChecker.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Checkers/Role/RoleDefaultChecker.php b/src/Checkers/Role/RoleDefaultChecker.php index 1f57afb6..29df7a01 100644 --- a/src/Checkers/Role/RoleDefaultChecker.php +++ b/src/Checkers/Role/RoleDefaultChecker.php @@ -64,12 +64,17 @@ public function currentRoleCachedPermissions(): array { $cacheKey = 'laratrust_permissions_for_role_'.$this->role->getKey(); - if (! Config::get('laratrust.cache.enabled')) { - return $this->role->permissions()->get()->toArray(); - } - - return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { - return $this->role->permissions()->get()->toArray(); - }); + return Cache::store('array') + ->rememberForever( + $cacheKey, + function() { + if (! Config::get('laratrust.cache.enabled')) { + return $this->role->permissions()->get()->toArray(); + } + + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { + return $this->role->permissions()->get()->toArray(); + }); + }); } } From 55c8e85ee80201703280e1e1a83890e0d7990642 Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 6 Aug 2024 13:07:36 +0200 Subject: [PATCH 3/8] Update RoleDefaultChecker.php --- src/Checkers/Role/RoleDefaultChecker.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Checkers/Role/RoleDefaultChecker.php b/src/Checkers/Role/RoleDefaultChecker.php index 29df7a01..5516f9f1 100644 --- a/src/Checkers/Role/RoleDefaultChecker.php +++ b/src/Checkers/Role/RoleDefaultChecker.php @@ -64,10 +64,9 @@ public function currentRoleCachedPermissions(): array { $cacheKey = 'laratrust_permissions_for_role_'.$this->role->getKey(); - return Cache::store('array') - ->rememberForever( + return Cache::store('array')->rememberForever( $cacheKey, - function() { + function() use ($cacheKey) { if (! Config::get('laratrust.cache.enabled')) { return $this->role->permissions()->get()->toArray(); } From 32cdbb931f83be58e6aa3ce3d5026807198c10d3 Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 6 Aug 2024 13:08:13 +0200 Subject: [PATCH 4/8] Update UserDefaultChecker.php --- src/Checkers/User/UserDefaultChecker.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Checkers/User/UserDefaultChecker.php b/src/Checkers/User/UserDefaultChecker.php index f73244e2..e615f8e1 100644 --- a/src/Checkers/User/UserDefaultChecker.php +++ b/src/Checkers/User/UserDefaultChecker.php @@ -154,7 +154,7 @@ protected function userCachedRoles(): array return Cache::store('array')->rememberForever( $cacheKey, - function() { + function() use ($cacheKey) { if (! Config::get('laratrust.cache.enabled')) { return $this->user->roles()->get()->toArray(); } @@ -176,7 +176,7 @@ public function userCachedPermissions(): array return Cache::store('array')->rememberForever( $cacheKey, - function() { + function() use ($cacheKey) { if (! Config::get('laratrust.cache.enabled')) { return $this->user->permissions()->get()->toArray(); } From 8391f5177218661b9bf29d4e82c871e9be053baf Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 6 Aug 2024 13:22:45 +0200 Subject: [PATCH 5/8] Update RoleDefaultChecker.php --- src/Checkers/Role/RoleDefaultChecker.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Checkers/Role/RoleDefaultChecker.php b/src/Checkers/Role/RoleDefaultChecker.php index 5516f9f1..df4cad96 100644 --- a/src/Checkers/Role/RoleDefaultChecker.php +++ b/src/Checkers/Role/RoleDefaultChecker.php @@ -65,15 +65,15 @@ public function currentRoleCachedPermissions(): array $cacheKey = 'laratrust_permissions_for_role_'.$this->role->getKey(); return Cache::store('array')->rememberForever( - $cacheKey, - function() use ($cacheKey) { - if (! Config::get('laratrust.cache.enabled')) { - return $this->role->permissions()->get()->toArray(); - } - - return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { - return $this->role->permissions()->get()->toArray(); - }); - }); + $cacheKey, + function() use ($cacheKey) { + if (! Config::get('laratrust.cache.enabled')) { + return $this->role->permissions()->get()->toArray(); + } + + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { + return $this->role->permissions()->get()->toArray(); + }); + }); } } From 222cba9a605644c19ef00852a358b713c6b12451 Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 6 Aug 2024 13:23:39 +0200 Subject: [PATCH 6/8] Update UserDefaultChecker.php --- src/Checkers/User/UserDefaultChecker.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Checkers/User/UserDefaultChecker.php b/src/Checkers/User/UserDefaultChecker.php index e615f8e1..394828ca 100644 --- a/src/Checkers/User/UserDefaultChecker.php +++ b/src/Checkers/User/UserDefaultChecker.php @@ -154,11 +154,11 @@ protected function userCachedRoles(): array return Cache::store('array')->rememberForever( $cacheKey, - function() use ($cacheKey) { + function () use ($cacheKey) { if (! Config::get('laratrust.cache.enabled')) { return $this->user->roles()->get()->toArray(); } - + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { return $this->user->roles()->get()->toArray(); }); @@ -176,11 +176,11 @@ public function userCachedPermissions(): array return Cache::store('array')->rememberForever( $cacheKey, - function() use ($cacheKey) { + function () use ($cacheKey) { if (! Config::get('laratrust.cache.enabled')) { return $this->user->permissions()->get()->toArray(); } - + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { return $this->user->permissions()->get()->toArray(); }); From 0f97a853d7b7387cdfdc8143ca0c9dbe4fd2a272 Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 6 Aug 2024 13:25:20 +0200 Subject: [PATCH 7/8] Update RoleDefaultChecker.php --- src/Checkers/Role/RoleDefaultChecker.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Checkers/Role/RoleDefaultChecker.php b/src/Checkers/Role/RoleDefaultChecker.php index df4cad96..63016a42 100644 --- a/src/Checkers/Role/RoleDefaultChecker.php +++ b/src/Checkers/Role/RoleDefaultChecker.php @@ -66,14 +66,14 @@ public function currentRoleCachedPermissions(): array return Cache::store('array')->rememberForever( $cacheKey, - function() use ($cacheKey) { + function () use ($cacheKey) { if (! Config::get('laratrust.cache.enabled')) { return $this->role->permissions()->get()->toArray(); } - + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { return $this->role->permissions()->get()->toArray(); }); - }); + }); } } From a408aeb40ee3a3578f46b8b068fb65935461dcd1 Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 6 Aug 2024 13:26:21 +0200 Subject: [PATCH 8/8] Update UserDefaultChecker.php --- src/Checkers/User/UserDefaultChecker.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Checkers/User/UserDefaultChecker.php b/src/Checkers/User/UserDefaultChecker.php index 394828ca..de43e791 100644 --- a/src/Checkers/User/UserDefaultChecker.php +++ b/src/Checkers/User/UserDefaultChecker.php @@ -158,7 +158,7 @@ function () use ($cacheKey) { if (! Config::get('laratrust.cache.enabled')) { return $this->user->roles()->get()->toArray(); } - + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { return $this->user->roles()->get()->toArray(); }); @@ -176,11 +176,11 @@ public function userCachedPermissions(): array return Cache::store('array')->rememberForever( $cacheKey, - function () use ($cacheKey) { + function () use ($cacheKey) { if (! Config::get('laratrust.cache.enabled')) { return $this->user->permissions()->get()->toArray(); } - + return Cache::remember($cacheKey, Config::get('laratrust.cache.expiration_time', 60), function () { return $this->user->permissions()->get()->toArray(); });