Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #7844

Closed
wants to merge 3 commits into from
Closed

test #7844

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions ProcessMaker/Cache/Settings/SettingCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Cache/SettingCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ public function testClearByPatternWithFailedDeletion()
\SettingCache::clearBy($pattern);
}

public function testTryClearByPatternWithNonRedisDriver()
/* 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()
{
Expand Down
Loading