From 050ecc15cbe88d07beb54dd5275042c9e2ad194b Mon Sep 17 00:00:00 2001 From: "Tais P. Hansen" Date: Wed, 7 May 2025 09:34:22 +0200 Subject: [PATCH] feat: add configurable cache store Allow user to choose the Laravel cache store used for caching. As Laravel's tagged cache implementation doesn't seem to work well with certain cache stores (like Redis), a user should be able to tell Entrust to use a compatible store. --- src/Entrust/Traits/EntrustRoleTrait.php | 27 +++++++++++++++---------- src/Entrust/Traits/EntrustUserTrait.php | 23 ++++++++++++--------- src/config/config.php | 11 ++++++++++ 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/Entrust/Traits/EntrustRoleTrait.php b/src/Entrust/Traits/EntrustRoleTrait.php index 4e39439a..ac3def6b 100644 --- a/src/Entrust/Traits/EntrustRoleTrait.php +++ b/src/Entrust/Traits/EntrustRoleTrait.php @@ -9,8 +9,8 @@ */ use Illuminate\Cache\TaggableStore; -use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Config; trait EntrustRoleTrait { @@ -19,8 +19,9 @@ public function cachedPermissions() { $rolePrimaryKey = $this->primaryKey; $cacheKey = 'entrust_permissions_for_role_' . $this->$rolePrimaryKey; - if (Cache::getStore() instanceof TaggableStore) { - return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl', 60), function () { + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + return $cache->tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl', 60), function () { return $this->perms()->get(); }); } else return $this->perms()->get(); @@ -31,8 +32,9 @@ public function save(array $options = []) if (!parent::save($options)) { return false; } - if (Cache::getStore() instanceof TaggableStore) { - Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + $cache->tags(Config::get('entrust.permission_role_table'))->flush(); } return true; } @@ -42,8 +44,9 @@ public function delete(array $options = []) if (!parent::delete($options)) { return false; } - if (Cache::getStore() instanceof TaggableStore) { - Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + $cache->tags(Config::get('entrust.permission_role_table'))->flush(); } return true; } @@ -53,8 +56,9 @@ public function restore() if (!parent::restore()) { return false; } - if (Cache::getStore() instanceof TaggableStore) { - Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + $cache->tags(Config::get('entrust.permission_role_table'))->flush(); } return true; } @@ -152,8 +156,9 @@ public function savePermissions($inputPermissions) $this->perms()->detach(); } - if (Cache::getStore() instanceof TaggableStore) { - Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + $cache->tags(Config::get('entrust.permission_role_table'))->flush(); } } diff --git a/src/Entrust/Traits/EntrustUserTrait.php b/src/Entrust/Traits/EntrustUserTrait.php index 67e7cbe8..77c0f23d 100644 --- a/src/Entrust/Traits/EntrustUserTrait.php +++ b/src/Entrust/Traits/EntrustUserTrait.php @@ -12,7 +12,6 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Config; use Illuminate\Support\Str; - use InvalidArgumentException; trait EntrustUserTrait @@ -26,8 +25,9 @@ public function cachedRoles() { $userPrimaryKey = $this->primaryKey; $cacheKey = 'entrust_roles_for_user_'.$this->$userPrimaryKey; - if(Cache::getStore() instanceof TaggableStore) { - return Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () { + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + return $cache->tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () { return $this->roles()->get(); }); } @@ -39,8 +39,9 @@ public function cachedRoles() */ public function save(array $options = []) { //both inserts and updates - if(Cache::getStore() instanceof TaggableStore) { - Cache::tags(Config::get('entrust.role_user_table'))->flush(); + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + $cache->tags(Config::get('entrust.role_user_table'))->flush(); } return parent::save($options); } @@ -51,8 +52,9 @@ public function save(array $options = []) public function delete(array $options = []) { //soft or hard $result = parent::delete($options); - if(Cache::getStore() instanceof TaggableStore) { - Cache::tags(Config::get('entrust.role_user_table'))->flush(); + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + $cache->tags(Config::get('entrust.role_user_table'))->flush(); } return $result; } @@ -63,8 +65,9 @@ public function delete(array $options = []) public function restore() { //soft delete undo's $result = parent::restore(); - if(Cache::getStore() instanceof TaggableStore) { - Cache::tags(Config::get('entrust.role_user_table'))->flush(); + $cache = Cache::store(Config::get('entrust.cache_store')); + if ($cache instanceof TaggableStore) { + $cache->tags(Config::get('entrust.role_user_table'))->flush(); } return $result; } @@ -307,7 +310,7 @@ public function detachRoles($roles=null) } /** - *Filtering users according to their role + *Filtering users according to their role * *@param string $role *@return users collection diff --git a/src/config/config.php b/src/config/config.php index 07da1773..c80a01be 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -10,6 +10,17 @@ return [ + /* + |-------------------------------------------------------------------------- + | Cache store used by Entrust + |-------------------------------------------------------------------------- + | + | The Laravel cache store used to cache permissions and roles. If null, it + | will use the default cache store. + | + */ + 'cache_store' => env('ENTRUST_CACHE_STORE', null), + /* |-------------------------------------------------------------------------- | Entrust Role Model