Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
fix: RedisHandler::deleteMatching() not deleting matching keys if cache prefix is used#8952
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
kenjis
merged 7 commits into
codeigniter4:develop
from
paulbalandan:redis-delete-matching-prefixJun 14, 2024
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f15535
Combine similar deleteMatching test cases
paulbalandan 3b7046a
Add failing test for cache prefix
paulbalandan e86b75a
Fix rector transform
paulbalandan 9099e20
Validate pattern against prefix
paulbalandan 3d5b97d
refactor RedisHandler::deleteMatching()
paulbalandan 766df8c
Add comments to each data
paulbalandan cb23bdb
Remove space
paulbalandan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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 |
|---|---|---|
| @@ -13,9 +13,11 @@ | ||
| namespace CodeIgniter\Cache\Handlers; | ||
| use CodeIgniter\Cache\CacheFactory; | ||
| use CodeIgniter\CLI\CLI; | ||
| use CodeIgniter\I18n\Time; | ||
| use Config\Cache; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| /** | ||
| @@ -129,44 +131,59 @@ public function testDelete(): void | ||
| $this->assertFalse($this->handler->delete(self::$dummy)); | ||
| } | ||
| public function testDeleteMatchingPrefix(): void | ||
| #[DataProvider('provideDeleteMatching')] | ||
| public function testDeleteMatching(string $pattern, int $expectedDeleteCount, string $prefix = ''): void | ||
| { | ||
| // Save 101 items to match on | ||
| $cache = new Cache(); | ||
| if ($prefix !== '') { | ||
| $cache->prefix = $prefix; | ||
| } | ||
| /** @var RedisHandler $handler */ | ||
| $handler = CacheFactory::getHandler($cache, 'redis'); | ||
| for ($i = 1; $i <= 101; $i++) { | ||
| $this->handler->save('key_' . $i, 'value' . $i); | ||
| $handler->save('key_' . $i, 'value_' . $i); | ||
| } | ||
| // check that there are 101 items is cache store | ||
| $dbInfo = explode(',', $this->handler->getCacheInfo()['db0']); | ||
| $this->assertSame('keys=101', $dbInfo[0]); | ||
| $cacheInfo = $handler->getCacheInfo(); | ||
| $this->assertIsArray($cacheInfo); | ||
| $this->assertArrayHasKey('db0', $cacheInfo); | ||
| $this->assertIsString($cacheInfo['db0']); | ||
| $this->assertMatchesRegularExpression('/^keys=(?P<count>\d+)/', $cacheInfo['db0']); | ||
| preg_match('/^keys=(?P<count>\d+)/', $cacheInfo['db0'], $matches); | ||
| $this->assertSame(101, (int) $matches['count']); | ||
| $this->assertSame($expectedDeleteCount, $handler->deleteMatching($pattern)); | ||
| // Checking that given the prefix "key_1", deleteMatching deletes 13 keys: | ||
| // (key_1, key_10, key_11, key_12, key_13, key_14, key_15, key_16, key_17, key_18, key_19, key_100, key_101) | ||
| $this->assertSame(13, $this->handler->deleteMatching('key_1*')); | ||
| $cacheInfo = $handler->getCacheInfo(); | ||
| $this->assertIsArray($cacheInfo); | ||
| $this->assertArrayHasKey('db0', $cacheInfo); | ||
| $this->assertIsString($cacheInfo['db0']); | ||
| $this->assertMatchesRegularExpression('/^keys=(?P<count>\d+)/', $cacheInfo['db0']); | ||
| // check that there remains (101 - 13) = 88 items is cache store | ||
| $dbInfo = explode(',', $this->handler->getCacheInfo()['db0']); | ||
| $this->assertSame('keys=88', $dbInfo[0]); | ||
| preg_match('/^keys=(?P<count>\d+)/', $cacheInfo['db0'], $matches); | ||
| $this->assertSame(101 - $expectedDeleteCount, (int) $matches['count']); | ||
| $handler->deleteMatching('key_*'); | ||
| } | ||
| public function testDeleteMatchingSuffix(): void | ||
| /** | ||
| * @return iterable<string, array{0: string, 1: int, 2?: string}> | ||
| */ | ||
| public static function provideDeleteMatching(): iterable | ||
| { | ||
| // Save 101 items to match on | ||
| for ($i = 1; $i <= 101; $i++) { | ||
| $this->handler->save('key_' . $i, 'value' . $i); | ||
| } | ||
| // check that there are 101 items is cache store | ||
| $dbInfo = explode(',', $this->handler->getCacheInfo()['db0']); | ||
| $this->assertSame('keys=101', $dbInfo[0]); | ||
| // Given the key "key_1*", deleteMatching() should delete 13 keys: | ||
| // key_1, key_10, key_11, key_12, key_13, key_14, key_15, key_16, key_17, key_18, key_19, key_100, key_101 | ||
| yield 'prefix' => ['key_1*', 13]; | ||
| // Checking that given the suffix "1", deleteMatching deletes 11 keys: | ||
| // (key_1, key_11, key_21, key_31, key_41, key_51, key_61, key_71, key_81, key_91, key_101) | ||
| $this->assertSame(11, $this->handler->deleteMatching('*1')); | ||
| // Given the key "*1", deleteMatching() should delete 11 keys: | ||
| // key_1, key_11, key_21, key_31, key_41, key_51, key_61, key_71, key_81, key_91, key_101 | ||
| yield 'suffix' => ['*1', 11]; | ||
paulbalandan marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // check that there remains (101 - 13) = 88 items is cache store | ||
| $dbInfo = explode(',', $this->handler->getCacheInfo()['db0']); | ||
| $this->assertSame('keys=90', $dbInfo[0]); | ||
| yield 'cache-prefix' => ['key_1*', 13, 'foo_']; | ||
| } | ||
| public function testIncrementAndDecrement(): void | ||
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.