Skip to content

Commit 2c61caa

Browse files
fix(cr): enhance SettingCacheManager to determine cache driver and improve clearBy method
1 parent 3370ea6 commit 2c61caa

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

ProcessMaker/Cache/Settings/SettingCacheManager.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,35 @@
33
namespace ProcessMaker\Cache\Settings;
44

55
use Illuminate\Cache\CacheManager;
6-
use Illuminate\Contracts\Cache\Repository;
76
use Illuminate\Support\Facades\Redis;
87
use ProcessMaker\Cache\CacheInterface;
98

109
class SettingCacheManager implements CacheInterface
1110
{
12-
protected Repository $cacheManager;
11+
const DEFAULT_CACHE_DRIVER = 'cache_settings';
12+
13+
protected CacheManager $cacheManager;
1314

1415
public function __construct(CacheManager $cacheManager)
1516
{
16-
$driver = env('CACHE_SETTING_DRIVER') ?? env('CACHE_DRIVER', 'redis');
17+
$driver = $this->determineCacheDriver();
1718

18-
$this->cacheManager = $cacheManager->store($driver);
19+
$this->cacheManager = $cacheManager;
20+
$this->cacheManager->store($driver);
21+
}
22+
23+
/**
24+
* Determine the cache driver to use.
25+
*
26+
* @return string
27+
*/
28+
private function determineCacheDriver(): string
29+
{
30+
$defaultCache = config('cache.default');
31+
if (in_array($defaultCache, ['redis', 'cache_settings'])) {
32+
return self::DEFAULT_CACHE_DRIVER;
33+
}
34+
return $defaultCache;
1935
}
2036

2137
/**
@@ -109,7 +125,7 @@ public function delete(string $key): bool
109125
*/
110126
public function clear(): bool
111127
{
112-
return $this->cacheManager->flush();
128+
return $this->cacheManager->clear();
113129
}
114130

115131
/**
@@ -122,11 +138,17 @@ public function clear(): bool
122138
*/
123139
public function clearBy(string $pattern): void
124140
{
141+
$defaultDriver = $this->cacheManager->getDefaultDriver();
142+
143+
if ($defaultDriver !== 'cache_settings') {
144+
throw new SettingCacheException('The cache driver must be Redis.');
145+
}
146+
125147
try {
126148
// get the connection name from the cache manager
127149
$connection = $this->cacheManager->connection()->getName();
128150
// Get all keys
129-
$keys = Redis::connection($connection)->keys('*');
151+
$keys = Redis::connection($connection)->keys($this->cacheManager->getPrefix() . '*');
130152
// Filter keys by pattern
131153
$matchedKeys = array_filter($keys, fn($key) => preg_match('/' . $pattern . '/', $key));
132154

tests/Feature/Cache/SettingCacheTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ protected function setUp(): void
2525
'is_administrator' => true,
2626
]);
2727

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

3131
protected function tearDown(): void
3232
{
3333
\SettingCache::clear();
3434

35-
putenv('CACHE_SETTING_DRIVER');
35+
config()->set('cache.default', 'array');
36+
3637
parent::tearDown();
3738
}
3839

@@ -205,6 +206,16 @@ public function testClearByPatternWithFailedDeletion()
205206
\SettingCache::clearBy($pattern);
206207
}
207208

209+
public function testTryClearByPatternWithNonRedisDriver()
210+
{
211+
config()->set('cache.default', 'array');
212+
213+
$this->expectException(SettingCacheException::class);
214+
$this->expectExceptionMessage('The cache driver must be Redis.');
215+
216+
\SettingCache::clearBy('pattern');
217+
}
218+
208219
public function testClearAllSettings()
209220
{
210221
\SettingCache::set('password-policies.users_can_change', 1);
@@ -226,16 +237,24 @@ public function testClearOnlySettings()
226237
{
227238
\SettingCache::set('password-policies.users_can_change', 1);
228239
\SettingCache::set('password-policies.numbers', 2);
240+
241+
config()->set('cache.default', 'array');
229242
Cache::put('password-policies.uppercase', 3);
230243

244+
config()->set('cache.default', 'cache_settings');
231245
$this->assertEquals(1, \SettingCache::get('password-policies.users_can_change'));
232246
$this->assertEquals(2, \SettingCache::get('password-policies.numbers'));
247+
248+
config()->set('cache.default', 'array');
233249
$this->assertEquals(3, Cache::get('password-policies.uppercase'));
234250

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

237254
$this->assertNull(\SettingCache::get('password-policies.users_can_change'));
238255
$this->assertNull(\SettingCache::get('password-policies.numbers'));
256+
257+
config()->set('cache.default', 'array');
239258
$this->assertEquals(3, Cache::get('password-policies.uppercase'));
240259
}
241260
}

0 commit comments

Comments
 (0)