Skip to content

Commit 8cab7b0

Browse files
authored
fix: prevent Redis error when deleteMatching finds no keys (#9829)
1 parent e4bbe32 commit 8cab7b0

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

system/Cache/Handlers/PredisHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public function deleteMatching(string $pattern)
162162
$matchedKeys[] = $key;
163163
}
164164

165+
if ($matchedKeys === []) {
166+
return 0;
167+
}
168+
165169
return $this->redis->del($matchedKeys);
166170
}
167171

system/Cache/Handlers/RedisHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public function deleteMatching(string $pattern)
195195
}
196196
} while ($iterator > 0);
197197

198-
return $this->redis->del($matchedKeys);
198+
return (int) $this->redis->del($matchedKeys);
199199
}
200200

201201
/**

tests/system/Cache/Handlers/PredisHandlerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ public function testDeleteMatchingSuffix(): void
160160
$this->assertSame('90', $this->handler->getCacheInfo()['Keyspace']['db0']['keys']);
161161
}
162162

163+
public function testDeleteMatchingNothing(): void
164+
{
165+
$this->assertSame(0, $this->handler->deleteMatching('user_1_info*'));
166+
}
167+
163168
public function testClean(): void
164169
{
165170
$this->handler->save(self::$key1, 1);

tests/system/Cache/Handlers/RedisHandlerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ public static function provideDeleteMatching(): iterable
184184
yield 'cache-prefix' => ['key_1*', 13, 'foo_'];
185185
}
186186

187+
public function testDeleteMatchingNothing(): void
188+
{
189+
$this->assertSame(0, $this->handler->deleteMatching('user_1_info*'));
190+
}
191+
187192
public function testIncrementAndDecrement(): void
188193
{
189194
$this->handler->save('counter', 100);

user_guide_src/source/changelogs/v4.6.4.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Deprecations
3737
Bugs Fixed
3838
**********
3939

40+
- **Cache:** Fixed a bug in ``PredisHandler::deleteMatching()`` causing Redis error when no keys match the pattern.
41+
- **Cache:** Fixed a bug in ``RedisHandler::deleteMatching()`` returning ``false`` instead of ``int`` when no keys match the pattern.
4042
- **Database:** Fixed a bug in ``Database::connect()`` which was causing to store non-shared connection instances in shared cache.
4143
- **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.
4244
- **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.

0 commit comments

Comments
 (0)