fix(redis): always prefix clear_pattern patterns that start with the key prefix - #156
Merged
Merged
Conversation
…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
This was referenced Sep 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #109.
Problem
AsyncRedisCacheBackend.clear_patternremoved 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. Withkey_prefix="cache:"and the defaultCacheManager(whose keys also start withcache:),CacheManager.clear_pattern("user:*")sendscache:user:*, which was stripped touser:*and cleared nothing, althoughcache:cache:user:1existed.The code has changed since the issue was written (it now uses
removeprefixrather than skipping the prefix), but the outcome is the same.Change
DeprecationWarningnames the pattern to pass instead. 0.4.0: remove the Redisclear_patternprefix fallback #125 removes the retry in 0.4.0.docs/BACKENDS.mdand the changelog (FixedandDeprecated) 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_patternand the glob-prefix test now expect theDeprecationWarning.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 --strictis clean.