Bug: deleteMatching() throws "ERR wrong number of arguments for 'del' command" on empty result set in PredisHandler
PHP Version
8.4
CodeIgniter4 Version
4.6.3
CodeIgniter4 Installation Method
Manual (zip or tar.gz)
Which operating systems have you tested for this bug?
Linux
Which server did you use?
apache
Database
No response
What happened?
When using cache()->deleteMatching('user_'. auth()->id() . '_*'); with Redis, if there are no matching keys, Redis throws the following error:
ERR wrong number of arguments for 'del' command
The issue occurs because CodeIgniter\Cache\Handlers\PredisHandler::deleteMatching() sends an empty array to Redis without checking if there are any matched keys.
Steps to Reproduce
- Use Redis as cache in a CodeIgniter 4 project.
- Call cache()->deleteMatching('user_'. auth()->id() . '_*'); where no keys match the pattern.
- Observe the error returned by Redis.
Expected Output
The method should return true or behave gracefully without sending an empty array to Redis, avoiding the error.
Anything else?
A simple fix is to add a check in deleteMatching() to return early if no keys are matched:
` public function deleteMatching(string $pattern) { $matchedKeys = [];
foreach (new Keyspace($this->redis, $pattern) as $key) {
$matchedKeys[] = $key;
}
if (empty($matchedKeys)) {
return true;
}
return $this->redis->del($matchedKeys);
} `
This prevents Redis from receiving an empty del command and resolves the issue.