Repository navigation
FOUR-21263 implement a general method to index cache keys #7836
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6612e70
feat: add the getKeysByPattern method
devmiguelangel af794c9
feat: enhance SettingCacheManager to extend CacheABC and improve cach…
devmiguelangel aec1487
feat: add console command to clear settings cache
devmiguelangel 88e6e76
refactor: rename CacheABC to CacheManagerBase
devmiguelangel df09a43
fix(cr): introduce CacheManagerException and update error handling in…
devmiguelangel 379c687
fix: improve cache driver handling
devmiguelangel 48f0e56
refactor: update SettingCacheManager
devmiguelangel 0218c1c
refactor: simplify cache driver handling
devmiguelangel 5f4c607
feat: enhance getKeysByPattern method to accept a connection
devmiguelangel 77994af
fix: update SettingCacheTest to use Cache facade for consistency
devmiguelangel 626adf1
fix: clean code
devmiguelangel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Cache; | ||
|
|
||
| use Exception; | ||
| use Illuminate\Support\Facades\Log; | ||
| use Illuminate\Support\Facades\Redis; | ||
|
|
||
| abstract class CacheManagerBase | ||
| { | ||
| /** | ||
| * The cache connection. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected string $connection; | ||
|
|
||
| /** | ||
| * The cache prefix. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected string $prefix; | ||
|
|
||
| /** | ||
| * The available cache connections. | ||
| * | ||
| * @var array | ||
| */ | ||
| protected const AVAILABLE_CONNECTIONS = ['redis', 'cache_settings']; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->connection = config('cache.default'); | ||
| $this->prefix = config('cache.prefix'); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve an array of cache keys that match a specific pattern. | ||
| * | ||
| * @param string $pattern The pattern to match. | ||
| * | ||
| * @return array An array of cache keys that match the pattern. | ||
| */ | ||
| public function getKeysByPattern(string $pattern): array | ||
| { | ||
| if (!in_array($this->connection, self::AVAILABLE_CONNECTIONS)) { | ||
| throw new Exception('`getKeysByPattern` method only supports Redis connections.'); | ||
| } | ||
|
|
||
| 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) { | ||
| Log::info('CacheABC' . $e->getMessage()); | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Console\Commands; | ||
|
|
||
| use Illuminate\Console\Command; | ||
|
|
||
| class CacheSettingClear extends Command | ||
| { | ||
| /** | ||
| * The name and signature of the console command. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $signature = 'cache:settings-clear'; | ||
|
|
||
| /** | ||
| * The console command description. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $description = 'Remove all of items from the settings cache'; | ||
|
|
||
| /** | ||
| * Execute the console command. | ||
| */ | ||
| public function handle() | ||
| { | ||
| \SettingCache::clear(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature\Cache; | ||
|
|
||
| use Exception; | ||
| use Illuminate\Support\Facades\Log; | ||
| use Illuminate\Support\Facades\Redis; | ||
| use ProcessMaker\Cache\CacheManagerBase; | ||
| use Tests\TestCase; | ||
|
|
||
| class CacheManagerBaseTest extends TestCase | ||
| { | ||
| protected $cacheManagerBase; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| config()->set('cache.default', 'redis'); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| config()->set('cache.default', 'array'); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testGetKeysByPatternWithValidConnectionAndMatchingKeys() | ||
| { | ||
| $this->cacheManagerBase = $this->getMockForAbstractClass(CacheManagerBase::class); | ||
|
|
||
| $pattern = 'test-pattern'; | ||
| $prefix = config('cache.prefix'); | ||
| $keys = [$prefix . ':test-pattern:1', $prefix . ':test-pattern:2']; | ||
|
|
||
| Redis::shouldReceive('connection') | ||
| ->with('redis') | ||
| ->andReturnSelf(); | ||
|
|
||
| Redis::shouldReceive('keys') | ||
| ->with($prefix . '*') | ||
| ->andReturn($keys); | ||
|
|
||
| $result = $this->cacheManagerBase->getKeysByPattern($pattern); | ||
|
|
||
| $this->assertCount(2, $result); | ||
| $this->assertEquals($keys, $result); | ||
| } | ||
|
|
||
| public function testGetKeysByPatternWithValidConnectionAndNoMatchingKeys() | ||
| { | ||
| $this->cacheManagerBase = $this->getMockForAbstractClass(CacheManagerBase::class); | ||
|
|
||
| $pattern = 'non-matching-pattern'; | ||
| $prefix = config('cache.prefix'); | ||
| $keys = [$prefix . ':test-pattern:1', $prefix . ':test-pattern:2']; | ||
|
|
||
| Redis::shouldReceive('connection') | ||
| ->with('redis') | ||
| ->andReturnSelf(); | ||
|
|
||
| Redis::shouldReceive('keys') | ||
| ->with($prefix . '*') | ||
| ->andReturn($keys); | ||
|
|
||
| $result = $this->cacheManagerBase->getKeysByPattern($pattern); | ||
|
|
||
| $this->assertCount(0, $result); | ||
| } | ||
|
|
||
| public function testGetKeysByPatternWithInvalidConnection() | ||
| { | ||
| config()->set('cache.default', 'array'); | ||
|
|
||
| $this->cacheManagerBase = $this->getMockForAbstractClass(CacheManagerBase::class); | ||
|
|
||
| $this->expectException(Exception::class); | ||
|
caleeli marked this conversation as resolved.
Resolved
|
||
| $this->expectExceptionMessage('`getKeysByPattern` method only supports Redis connections.'); | ||
|
|
||
| $this->cacheManagerBase->getKeysByPattern('pattern'); | ||
| } | ||
|
|
||
| public function testGetKeysByPatternWithExceptionDuringKeyRetrieval() | ||
| { | ||
| $this->cacheManagerBase = $this->getMockForAbstractClass(CacheManagerBase::class); | ||
|
|
||
| $pattern = 'test-pattern'; | ||
| $prefix = config('cache.prefix'); | ||
|
|
||
| Redis::shouldReceive('connection') | ||
| ->with('redis') | ||
| ->andReturnSelf(); | ||
|
|
||
| Redis::shouldReceive('keys') | ||
| ->with($prefix . '*') | ||
| ->andThrow(new Exception('Redis error')); | ||
|
|
||
| Log::shouldReceive('info') | ||
| ->with('CacheABC' . 'Redis error') | ||
|
caleeli marked this conversation as resolved.
Resolved
|
||
| ->once(); | ||
|
|
||
| $result = $this->cacheManagerBase->getKeysByPattern($pattern); | ||
|
|
||
| $this->assertCount(0, $result); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.