Repository navigation
fix(clear): delete only the requested key and enforce namespace boundaries - #7
Merged
Merged
Conversation
…aries Fix FastAPICache.clear(key=...) so it deletes only that key (long2ice#451/long2ice#529). Require the ':' boundary for InMemory and Redis namespace clears (long2ice#578). Replace Redis KEYS/Lua with SCAN/UNLINK; Redis Cluster namespace clear raises NotImplementedError. Add InMemory and Redis 7 integration tests, Redis Cluster unit tests, and migration notes.
Install the redis extra and start a Redis 7 service so changed-line coverage includes the Redis clear code.
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.
Summary
Fix
FastAPICache.clearsoclear(key=...)deletes only the requested key and namespaceclears require the
:boundary. Redis standalone now usesSCAN+UNLINK; Redis Clusternamespace clearing is explicitly unsupported.
Changes
FastAPICache.clear:clear(key=...)passes only the key to the backend, fixing Can't clear Redis by key if a prefix has been set long2ice/fastapi-cache#451/fix: .clear method generates namespace even if key is passed long2ice/fastapi-cache#529.clear(namespace=...)keeps the configured prefix and requires the namespace boundary.clear()with an empty prefix is a documented no-op.namespaceandkeyare provided,namespacewins.InMemoryBackend.clear:namespace + ":"instead of an arbitrary prefix (Fix in-memory cache namespace invalidation boundaries long2ice/fastapi-cache#578).RedisBackend.clear:KEYSand Lua string interpolation withSCAN+UNLINK.decode_responses=FalseandTrue.UNLINKresult and caps batch sizes.NotImplementedError; key-only clear works.NotImplementedErrorin0.3.0a1.tests/test_clear_semantics.pyandtests/test_redis_clear.py.redis-clearCI job with a Redis 7 service. The job fails if the Redis tests areskipped or not collected.
COMPATIBILITY.md,MIGRATION.md,CONTRIBUTING.md, and add a changelog fragment.Verification
tests/test_clear_semantics.py-> 12 failed, 9 passed.(Python 3.8/3.9 + Pydantic 2.8.2); 3.10/3.11 + Pydantic 1.10.27: 60 passed / 18 skipped.
diff-cover100% on changed lines.ruff check .,pre-commit run --all-files, mypy changed files,uv buildandtwine checkpass.Compatibility notes
clear(key=...)no longer deletes the whole prefix; pass the full key.clear("app:user")no longer deletesapp:users; use the exact namespace.NotImplementedError; use key-only clear orflush the database out of band.
clear()is an administrative operation; do not expose it to untrusted input.Checklist
Refs long2ice#451, long2ice#529, long2ice#578.