refactor RedisHandler::deleteMatching()

This commit is contained in:
John Paul E. Balandan, CPA 2024-06-10 21:55:21 +08:00
parent 9099e20cae
commit 3d5b97d582
No known key found for this signature in database
GPG Key ID: 697D84680E3738DA

View File

@ -175,19 +175,17 @@ class RedisHandler extends BaseHandler
*/
public function deleteMatching(string $pattern)
{
/** @var list<string> $matchedKeys */
$matchedKeys = [];
$pattern = static::validateKey($pattern, $this->prefix);
$iterator = null;
do {
// Scan for some keys
/** @var false|list<string>|Redis $keys */
$keys = $this->redis->scan($iterator, $pattern);
// Redis may return empty results, so protect against that
if ($keys !== false) {
foreach ($keys as $key) {
$matchedKeys[] = $key;
}
if (is_array($keys)) {
$matchedKeys = [...$matchedKeys, ...$keys];
}
} while ($iterator > 0);