Skip to content

fix(redis): always prefix clear_pattern patterns that start with the key prefix - #156

Merged
allen0099 merged 1 commit into
masterfrom
fix/redis-clear-pattern-prefix
Sep 26, 2026
Merged

allen0099 merged 1 commit into
masterfrom
fix/redis-clear-pattern-prefix

Conversation

@allen0099

Copy link
Copy Markdown
Owner

Closes #109.

Problem

AsyncRedisCacheBackend.clear_pattern removed the backend key prefix from any pattern that started with it. A logical key that itself starts with the prefix could therefore never be matched. With key_prefix="cache:" and the default CacheManager (whose keys also start with cache:), CacheManager.clear_pattern("user:*") sends cache:user:*, which was stripped to user:* and cleared nothing, although cache:cache:user:1 existed.

The code has changed since the issue was written (it now uses removeprefix rather than skipping the prefix), but the outcome is the same.

Change

  • The pattern always matches the logical key: the escaped prefix is always added, as on the memory backend and as the base docstring says.
  • Compatibility: when that clears nothing and the pattern starts with the prefix, the old stripped form is tried. If it clears anything, a DeprecationWarning names the pattern to pass instead. 0.4.0: remove the Redis clear_pattern prefix fallback #125 removes the retry in 0.4.0.
  • docs/BACKENDS.md and the changelog (Fixed and Deprecated) describe both.

One edge case keeps the 0.3.7 behaviour: a pattern that starts with the prefix and matches nothing under the new rule still falls back to the stripped form. That is exactly what 0.3.7 did for every such pattern, and it now warns.

Tests

  • test_redis_cache_manager_clear_pattern_with_matching_prefixes: the reproduction from the issue.
  • test_redis_clear_pattern_prefixes_a_pattern_that_starts_with_the_prefix: a key that starts with the prefix is cleared, a key without it is kept, and no warning is emitted.
  • test_redis_clear_pattern_with_prefixed_pattern and the glob-prefix test now expect the DeprecationWarning.

With the fix reverted, those four tests fail. The full suite passes against live Redis and Memcached (CACHEX_REQUIRE_LIVE_SERVERS=1): 792 passed. mypy --strict is clean.

…key prefix

clear_pattern stripped the backend key prefix from a pattern that started
with it, so a logical key that itself starts with the prefix could not be
matched. With key_prefix="cache:" and the default CacheManager, whose keys
also start with "cache:", CacheManager.clear_pattern("user:*") cleared
nothing.

The pattern now always matches the logical key, as on the memory backend.
When it clears nothing and starts with the prefix, the old stripped form is
still tried and emits a DeprecationWarning if it clears anything; #125
removes that retry in 0.4.0.

Closes #109
@allen0099 allen0099 added this to the 0.3.8 milestone Sep 26, 2026
@allen0099 allen0099 added bug Something isn't working backends Cache backends and their atomic primitives labels Sep 26, 2026
@allen0099
allen0099 merged commit 674be4c into master Sep 26, 2026
11 checks passed
@allen0099
allen0099 deleted the fix/redis-clear-pattern-prefix branch September 26, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backends Cache backends and their atomic primitives bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redis clear_pattern does not prefix patterns that already start with the key prefix

1 participant