Skip to content

Bug: deleteMatching() throws "ERR wrong number of arguments for 'del' command" on empty result set in PredisHandler #9828

@mbnl

Description

@mbnl

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

  1. Use Redis as cache in a CodeIgniter 4 project.
  2. Call cache()->deleteMatching('user_'. auth()->id() . '_*'); where no keys match the pattern.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions