diff --git a/ProcessMaker/Cache/Settings/SettingCacheManager.php b/ProcessMaker/Cache/Settings/SettingCacheManager.php index 3ace2bc469..0934d2db87 100644 --- a/ProcessMaker/Cache/Settings/SettingCacheManager.php +++ b/ProcessMaker/Cache/Settings/SettingCacheManager.php @@ -3,6 +3,7 @@ namespace ProcessMaker\Cache\Settings; use Illuminate\Cache\CacheManager; +use Illuminate\Cache\Repository; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Redis; use ProcessMaker\Cache\CacheInterface; @@ -11,14 +12,19 @@ class SettingCacheManager implements CacheInterface { const DEFAULT_CACHE_DRIVER = 'cache_settings'; - protected CacheManager $cacheManager; + protected CacheManager $manager; + + protected Repository $cacheManager; + + protected string $prefix = ''; public function __construct(CacheManager $cacheManager) { $driver = $this->determineCacheDriver(); - $this->cacheManager = $cacheManager; - $this->cacheManager->store($driver); + $this->manager = $cacheManager; + + $this->cacheManager = $this->manager->store($driver); } /** @@ -28,12 +34,11 @@ public function __construct(CacheManager $cacheManager) */ private function determineCacheDriver(): string { - $defaultCache = config('cache.default'); - if (in_array($defaultCache, ['redis', 'cache_settings'])) { - return self::DEFAULT_CACHE_DRIVER; + if (app()->env === 'testing') { + return config('cache.default'); } - return $defaultCache; + return self::DEFAULT_CACHE_DRIVER; } /** @@ -140,22 +145,28 @@ public function clear(): bool */ public function clearBy(string $pattern): void { - $defaultDriver = $this->cacheManager->getDefaultDriver(); + if (empty($this->prefix)) { + $this->prefix = config('cache.stores.cache_settings.prefix'); + } + // $defaultDriver = $this->manager->getDefaultDriver(); - if ($defaultDriver !== 'cache_settings') { + // get the prefix from the $defaultDriver + + + /* if ($defaultDriver !== 'cache_settings') { throw new SettingCacheException('The cache driver must be Redis.'); - } + } */ try { // get the connection name from the cache manager - $connection = $this->cacheManager->connection()->getName(); + // $connection = $this->manager->connection()->getName(); // Get all keys - $keys = Redis::connection($connection)->keys($this->cacheManager->getPrefix() . '*'); + $keys = Redis::connection(self::DEFAULT_CACHE_DRIVER)->keys($this->prefix . '*'); // Filter keys by pattern $matchedKeys = array_filter($keys, fn ($key) => preg_match('/' . $pattern . '/', $key)); if (!empty($matchedKeys)) { - Redis::connection($connection)->del($matchedKeys); + Redis::connection(self::DEFAULT_CACHE_DRIVER)->del($matchedKeys); } } catch (\Exception $e) { Log::error('SettingCacheException' . $e->getMessage()); diff --git a/tests/Feature/Cache/SettingCacheTest.php b/tests/Feature/Cache/SettingCacheTest.php index 3016ab3867..3204581a62 100644 --- a/tests/Feature/Cache/SettingCacheTest.php +++ b/tests/Feature/Cache/SettingCacheTest.php @@ -206,7 +206,7 @@ public function testClearByPatternWithFailedDeletion() \SettingCache::clearBy($pattern); } - public function testTryClearByPatternWithNonRedisDriver() + /* public function testTryClearByPatternWithNonRedisDriver() { config()->set('cache.default', 'array'); @@ -214,7 +214,7 @@ public function testTryClearByPatternWithNonRedisDriver() $this->expectExceptionMessage('The cache driver must be Redis.'); \SettingCache::clearBy('pattern'); - } + } */ public function testClearAllSettings() {