diff --git a/system/Cache/Handlers/PredisHandler.php b/system/Cache/Handlers/PredisHandler.php index 250ea74a88e0..8d0ce9998807 100644 --- a/system/Cache/Handlers/PredisHandler.php +++ b/system/Cache/Handlers/PredisHandler.php @@ -162,6 +162,10 @@ public function deleteMatching(string $pattern) $matchedKeys[] = $key; } + if ($matchedKeys === []) { + return 0; + } + return $this->redis->del($matchedKeys); } diff --git a/system/Cache/Handlers/RedisHandler.php b/system/Cache/Handlers/RedisHandler.php index 3a13ee07f8e4..06a089e2660b 100644 --- a/system/Cache/Handlers/RedisHandler.php +++ b/system/Cache/Handlers/RedisHandler.php @@ -195,7 +195,7 @@ public function deleteMatching(string $pattern) } } while ($iterator > 0); - return $this->redis->del($matchedKeys); + return (int) $this->redis->del($matchedKeys); } /** diff --git a/tests/system/Cache/Handlers/PredisHandlerTest.php b/tests/system/Cache/Handlers/PredisHandlerTest.php index eec292f280ad..96857fc6a9f2 100644 --- a/tests/system/Cache/Handlers/PredisHandlerTest.php +++ b/tests/system/Cache/Handlers/PredisHandlerTest.php @@ -160,6 +160,11 @@ public function testDeleteMatchingSuffix(): void $this->assertSame('90', $this->handler->getCacheInfo()['Keyspace']['db0']['keys']); } + public function testDeleteMatchingNothing(): void + { + $this->assertSame(0, $this->handler->deleteMatching('user_1_info*')); + } + public function testClean(): void { $this->handler->save(self::$key1, 1); diff --git a/tests/system/Cache/Handlers/RedisHandlerTest.php b/tests/system/Cache/Handlers/RedisHandlerTest.php index 467abc675372..47c92aaff372 100644 --- a/tests/system/Cache/Handlers/RedisHandlerTest.php +++ b/tests/system/Cache/Handlers/RedisHandlerTest.php @@ -184,6 +184,11 @@ public static function provideDeleteMatching(): iterable yield 'cache-prefix' => ['key_1*', 13, 'foo_']; } + public function testDeleteMatchingNothing(): void + { + $this->assertSame(0, $this->handler->deleteMatching('user_1_info*')); + } + public function testIncrementAndDecrement(): void { $this->handler->save('counter', 100); diff --git a/user_guide_src/source/changelogs/v4.6.4.rst b/user_guide_src/source/changelogs/v4.6.4.rst index 1f32d3727b27..2ea5a8dd4b59 100644 --- a/user_guide_src/source/changelogs/v4.6.4.rst +++ b/user_guide_src/source/changelogs/v4.6.4.rst @@ -37,6 +37,8 @@ Deprecations Bugs Fixed ********** +- **Cache:** Fixed a bug in ``PredisHandler::deleteMatching()`` causing Redis error when no keys match the pattern. +- **Cache:** Fixed a bug in ``RedisHandler::deleteMatching()`` returning ``false`` instead of ``int`` when no keys match the pattern. - **Database:** Fixed a bug in ``Database::connect()`` which was causing to store non-shared connection instances in shared cache. - **Database:** Fixed a bug in ``Connection::getFieldData()`` for ``SQLSRV`` and ``OCI8`` where extra characters were returned in column default values (specific to those handlers), instead of following the convention used by other drivers. - **Database:** Fixed a bug in ``BaseBuilder::compileOrderBy()`` where the method could overwrite ``QBOrderBy`` with a string instead of keeping it as an array, causing type errors and preventing additional ``ORDER BY`` clauses from being appended.