Skip to content

fix(clear): delete only the requested key and enforce namespace boundaries - #7

Merged
agustin18 merged 2 commits into
stablefrom
fix/clear-semantics
Nov 14, 2026
Merged

agustin18 merged 2 commits into
stablefrom
fix/clear-semantics

Conversation

@agustin18

@agustin18 agustin18 commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fix FastAPICache.clear so clear(key=...) deletes only the requested key and namespace
clears require the : boundary. Redis standalone now uses SCAN + UNLINK; Redis Cluster
namespace clearing is explicitly unsupported.

Changes

  • FastAPICache.clear:
  • InMemoryBackend.clear:
  • RedisBackend.clear:
    • Replaces KEYS and Lua string interpolation with SCAN + UNLINK.
    • Escapes glob metacharacters and filters the boundary in Python.
    • Handles decode_responses=False and True.
    • Counts the actual UNLINK result and caps batch sizes.
    • Redis Cluster namespace clear raises NotImplementedError; key-only clear works.
  • Memcached/DynamoDB clear remains NotImplementedError in 0.3.0a1.
  • Add tests/test_clear_semantics.py and tests/test_redis_clear.py.
  • Add a redis-clear CI job with a Redis 7 service. The job fails if the Redis tests are
    skipped or not collected.
  • Update COMPATIBILITY.md, MIGRATION.md, CONTRIBUTING.md, and add a changelog fragment.

Verification

  • RED before implementation: tests/test_clear_semantics.py -> 12 failed, 9 passed.
  • GREEN with Redis 7: 78 passed on Python 3.10-3.13.
  • Legacy: 60 passed / 18 skipped (Python 3.8/3.9 + Pydantic 1.10.27); 66 passed / 12 skipped
    (Python 3.8/3.9 + Pydantic 2.8.2); 3.10/3.11 + Pydantic 1.10.27: 60 passed / 18 skipped.
  • Redis 7 integration: 12 passed.
  • diff-cover 100% on changed lines.
  • ruff check ., pre-commit run --all-files, mypy changed files, uv build and
    twine check pass.

Compatibility notes

  • clear(key=...) no longer deletes the whole prefix; pass the full key.
  • clear("app:user") no longer deletes app:users; use the exact namespace.
  • Redis Cluster namespace clear now raises NotImplementedError; use key-only clear or
    flush the database out of band.
  • Namespace clear is best-effort, not atomic.
  • clear() is an administrative operation; do not expose it to untrusted input.
  • No cache key format change.

Checklist

  • Tests RED -> GREEN
  • InMemory and Redis 7 integration tests
  • Redis Cluster unit tests
  • Changed-line coverage 100%
  • Lint, typecheck, build, twine
  • Docs and migration notes
  • Changelog fragment

Refs long2ice#451, long2ice#529, long2ice#578.

…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.
@agustin18
agustin18 merged commit 53d3132 into stable Nov 14, 2026
20 checks passed
@agustin18
agustin18 deleted the fix/clear-semantics branch November 14, 2026 00:17
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