fix(redis): match the key prefix and clear_path path literally in SCAN - #139
Merged
Merged
Conversation
SCAN MATCH patterns interpolated key_prefix and the clear_path path raw, so
glob characters in them were live: clear_path("/files/[draft]") missed the
entry for that path, and a prefix with ? or * reached other prefixes' keys.
Escape *?[]\ in both; only the clear_pattern argument stays a glob.
Closes #106
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 #106
Problem
AsyncRedisCacheBackendbuilt itsSCAN MATCHpatterns by interpolatingkey_prefixand theclear_path()path directly, so glob metacharacters in them were live:clear_path("/files/[draft]")read[draft]as a character class and missed the cached HTTP entry for that path. The memory backend cleared it.key_prefixcontaining?or*letclear(),get_all_keys()(and soget_cache_data()) andclear_pattern()reach keys under other prefixes on the same server.Change
_escape_glob()backslash-escapes*?[]\for Redis glob patterns.clear,get_all_keys,clear_pathandclear_pattern.clear_path.clear_pattern(pattern)now always builds the pattern as escaped prefix +pattern. A pattern that already repeats the prefix has it stripped first, so both call forms keep working. Only the caller's pattern stays a live glob.docs/BACKENDS.mdgains a note on this, and there is a CHANGELOG entry under Fixed.Not breaking.
Out of scope
CacheManager.clear_pattern()still puts its ownkey_prefix(cache:by default) into the pattern it hands to the backend, so glob characters in a custom manager prefix remain live. The escaping syntax differs per backend (Redis uses backslashes, the memory backend'sfnmatchuses[*]), so fixing that needs its own design.Tests
The new live Redis tests in
tests/backends/test_redis.pyfail on the old code and pass with this change:clear_pathon a path containing[]*?\removes its exact and query-param entries and leaves look-alike keys that the unescaped pattern would have matched.cachex-test?*:does not list or delete keys of acachex-testX-other:backend throughget_all_keys,clear_pattern(with and without the prefix) orclear.Full suite with live Redis and Memcached: 761 passed. The docs build passes with
--strict.