Skip to content

fix: handle key prefixes and glob metacharacters when clearing Redis queues - #105

Open
gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:fix/redis-predis-clear-prefix-and-glob
Open

gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:fix/redis-predis-clear-prefix-and-glob

Conversation

@gr8man

@gr8man gr8man commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Description

RedisHandler::clear() and PredisHandler::clear() were unreliable in two ways.

  1. Key prefixes prevented deletion. Both KEYS and SCAN-less key listing return fully qualified key names, including the client key prefix. DEL is then processed by the client, which prepends the prefix again to every key it receives (phpredis Redis::OPT_PREFIX, Predis prefix option). With a prefix configured, the resulting names did not exist, no key was deleted, and clear() returned false — while queue:clear still printed a success message and exited with EXIT_SUCCESS.
  2. Queue names were interpolated into a KEYS glob. A queue name such as * or products* expanded the pattern far beyond the requested queue and could delete unrelated queues. Queue names are validated on push(), so clear() should validate them as well.

Changes

  • src/Handlers/RedisHandler.php
    • Validate the queue name with the existing validateQueue() before building the queues:{queue}:* pattern (invalid names are rejected, the error is logged, and false is returned).
    • Read Redis::OPT_PREFIX, strip it from every key returned by KEYS before passing the keys to DEL.
    • Check the return values of KEYS and DEL; log an error and return false when they fail.
    • Catch Throwable from the client, log it and return false instead of failing the surrounding command.
    • Emit queueCleared only when the clear actually succeeded.
  • src/Handlers/PredisHandler.php
    • Same behaviour, using KeyPrefixProcessor from the Predis options to obtain the prefix.
  • src/Commands/QueueClear.php
    • Return EXIT_ERROR and print an error message when clear() reports a failure, instead of always reporting success.

Testing

  • tests/RedisHandlerTest.php and tests/PredisHandlerTest.php
    • testClearWithPrefix() — keys are deleted exactly once and the prefix is not applied twice.
    • testClearWithInvalidQueueNameReturnsFalse() — glob metacharacters in the queue name are rejected.
  • tests/Commands/QueueClearTest.php
    • testRunFailedWhenClearReturnsFalse() — the command exits with EXIT_ERROR and reports the failure.

Verified locally on PHP 8.5 with PHPUnit 11.5:

phpunit ....... OK (235 tests, 669 assertions, 27 skipped)
phpstan ....... [OK] No errors
psalm ......... No errors found
php-cs-fixer .. 0 of 75 files would be fixed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant