Skip to content

Commit

Permalink
fix(cr): enhance SettingCacheManager to determine cache driver and im…
Browse files Browse the repository at this point in the history
…prove clearBy method
  • Loading branch information
devmiguelangel committed Dec 5, 2024
1 parent 3370ea6 commit 2c61caa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
34 changes: 28 additions & 6 deletions ProcessMaker/Cache/Settings/SettingCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@
namespace ProcessMaker\Cache\Settings;

use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Facades\Redis;
use ProcessMaker\Cache\CacheInterface;

class SettingCacheManager implements CacheInterface
{
protected Repository $cacheManager;
const DEFAULT_CACHE_DRIVER = 'cache_settings';

protected CacheManager $cacheManager;

public function __construct(CacheManager $cacheManager)
{
$driver = env('CACHE_SETTING_DRIVER') ?? env('CACHE_DRIVER', 'redis');
$driver = $this->determineCacheDriver();

$this->cacheManager = $cacheManager->store($driver);
$this->cacheManager = $cacheManager;
$this->cacheManager->store($driver);
}

/**
* Determine the cache driver to use.
*
* @return string
*/
private function determineCacheDriver(): string
{
$defaultCache = config('cache.default');
if (in_array($defaultCache, ['redis', 'cache_settings'])) {
return self::DEFAULT_CACHE_DRIVER;
}
return $defaultCache;
}

/**
Expand Down Expand Up @@ -109,7 +125,7 @@ public function delete(string $key): bool
*/
public function clear(): bool
{
return $this->cacheManager->flush();
return $this->cacheManager->clear();
}

/**
Expand All @@ -122,11 +138,17 @@ public function clear(): bool
*/
public function clearBy(string $pattern): void
{
$defaultDriver = $this->cacheManager->getDefaultDriver();

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();
// Get all keys
$keys = Redis::connection($connection)->keys('*');
$keys = Redis::connection($connection)->keys($this->cacheManager->getPrefix() . '*');
// Filter keys by pattern
$matchedKeys = array_filter($keys, fn($key) => preg_match('/' . $pattern . '/', $key));

Expand Down
23 changes: 21 additions & 2 deletions tests/Feature/Cache/SettingCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ protected function setUp(): void
'is_administrator' => true,
]);

putenv('CACHE_SETTING_DRIVER=cache_settings');
config()->set('cache.default', 'cache_settings');
}

protected function tearDown(): void
{
\SettingCache::clear();

putenv('CACHE_SETTING_DRIVER');
config()->set('cache.default', 'array');

parent::tearDown();
}

Expand Down Expand Up @@ -205,6 +206,16 @@ public function testClearByPatternWithFailedDeletion()
\SettingCache::clearBy($pattern);
}

public function testTryClearByPatternWithNonRedisDriver()
{
config()->set('cache.default', 'array');

$this->expectException(SettingCacheException::class);
$this->expectExceptionMessage('The cache driver must be Redis.');

\SettingCache::clearBy('pattern');
}

public function testClearAllSettings()
{
\SettingCache::set('password-policies.users_can_change', 1);
Expand All @@ -226,16 +237,24 @@ public function testClearOnlySettings()
{
\SettingCache::set('password-policies.users_can_change', 1);
\SettingCache::set('password-policies.numbers', 2);

config()->set('cache.default', 'array');
Cache::put('password-policies.uppercase', 3);

config()->set('cache.default', 'cache_settings');
$this->assertEquals(1, \SettingCache::get('password-policies.users_can_change'));
$this->assertEquals(2, \SettingCache::get('password-policies.numbers'));

config()->set('cache.default', 'array');
$this->assertEquals(3, Cache::get('password-policies.uppercase'));

config()->set('cache.default', 'cache_settings');
\SettingCache::clear();

$this->assertNull(\SettingCache::get('password-policies.users_can_change'));
$this->assertNull(\SettingCache::get('password-policies.numbers'));

config()->set('cache.default', 'array');
$this->assertEquals(3, Cache::get('password-policies.uppercase'));
}
}

0 comments on commit 2c61caa

Please sign in to comment.