Skip to content

Commit

Permalink
feat: enhance getKeysByPattern method to accept a connection
Browse files Browse the repository at this point in the history
  • Loading branch information
devmiguelangel committed Dec 18, 2024
1 parent 0218c1c commit 1b514b3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
14 changes: 13 additions & 1 deletion ProcessMaker/Cache/CacheManagerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,33 @@ public function __construct()
* Retrieve an array of cache keys that match a specific pattern.
*
* @param string $pattern The pattern to match.
* @param string|null $connection The cache connection to use.
*
* @return array An array of cache keys that match the pattern.
*/
public function getKeysByPattern(string $pattern): array
public function getKeysByPattern(string $pattern, string $connection = null, string $prefix = null): array
{
if ($connection) {
$this->connection = $connection;
}

if ($prefix) {
$this->prefix = $prefix;
}

if (!in_array($this->connection, self::AVAILABLE_CONNECTIONS)) {
throw new CacheManagerException('`getKeysByPattern` method only supports Redis connections.');
}

dump('getKeysByPattern prefix => ' . $this->connection . ' ' . $this->prefix);

try {
// Get all keys
$keys = Redis::connection($this->connection)->keys($this->prefix . '*');
// Filter keys by pattern
return array_filter($keys, fn ($key) => preg_match('/' . $pattern . '/', $key));
} catch (Exception $e) {
dump('getKeysByPattern => ' . $e->getMessage());
Log::info('CacheManagerBase: ' . $e->getMessage());
}

Expand Down
2 changes: 1 addition & 1 deletion ProcessMaker/Cache/Settings/SettingCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function clearBy(string $pattern): void

try {
// Filter keys by pattern
$matchedKeys = $this->getKeysByPattern($pattern);
$matchedKeys = $this->getKeysByPattern($pattern, $defaultDriver, $this->manager->getPrefix());

if (!empty($matchedKeys)) {
Redis::connection($defaultDriver)->del($matchedKeys);
Expand Down
2 changes: 2 additions & 0 deletions ProcessMaker/Console/Commands/CacheSettingClear.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ class CacheSettingClear extends Command
public function handle()
{
\SettingCache::clear();

$this->info('Settings cache cleared.');
}
}
11 changes: 3 additions & 8 deletions tests/Feature/Cache/SettingCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,19 @@ 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);
Cache::store('file')->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'));
$this->assertEquals(3, Cache::store('file')->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'));
$this->assertEquals(3, Cache::store('file')->get('password-policies.uppercase'));
}

public function testInvalidateOnSaved()
Expand Down

0 comments on commit 1b514b3

Please sign in to comment.