Skip to content

Fix in-memory cache namespace invalidation boundaries - #578

Open
anishpentyala wants to merge 5 commits into
long2ice:mainfrom
anishpentyala:fix/inmemory-namespace-boundaries
Open

anishpentyala wants to merge 5 commits into
long2ice:mainfrom
anishpentyala:fix/inmemory-namespace-boundaries

Conversation

@anishpentyala

Copy link
Copy Markdown

Problem

InMemoryBackend.clear(namespace=...) matches the namespace as an arbitrary string prefix. With the default key builder, clearing app:user also removes cached entries in app:users and app:username. Clearing the entire app prefix can similarly remove entries belonging to application in the shared in-memory store.

Change

Require the colon separator after the namespace, matching the existing Redis backend convention. Nested namespaces are still included. Add direct backend boundary tests, decorated-function tests for FastAPICache.clear, and controls for key-only clearing and no-argument behavior. Include an unnumbered towncrier fragment, verified with the project's towncrier 22.12.0 draft builder.

Compatibility note: a raw custom key exactly equal to the namespace, with no following colon, is now retained. Such a key is also not matched by Redis's existing namespace:* behavior. Custom key-builder formats, Redis glob-metacharacter behavior, expiry, and key-vs-namespace argument precedence are not changed. This is separate from #529 and #571.

Verification

Actual Python 3.8.20, 3.10.12, 3.11.17, and 3.13.17:

  • New tests against unchanged main: 9 failed, 3 passed. Failures show namespace overdeletion.
  • New tests with this change: 12 passed.
  • Full unchanged-main suite: 19 passed, 6 failed in the installed Pydantic 2.x environments.
  • Full suite with this change: 31 passed, the same 6 failed. Four existing typed-JSON codec failures, the datetime equality assertion, and the method/route assertion remain. This is not a full-green claim.
  • Ruff 0.1.1 repository checks pass before and after; git diff --check passes.
  • Independent review executed the focused negative/control comparison and checked exact source/tree identity. Its Pydantic 1.x full-suite run had one fewer existing codec failure, so dependency versions affect the preexisting failure count.

No external backend service, mypy, or pyright run is claimed.

Fixes an issue with in-memory namespace invalidation that incorrectly deleted sibling namespaces or application prefixes sharing the same initial text. Updates namespace matching to require a colon separator, aligning with Redis backend behavior.
agustin18 added a commit to agustin18/fastapi-cache that referenced this pull request Nov 14, 2026
…aries (#7)

* fix(clear): delete only the requested key and enforce namespace boundaries

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.

* ci: run Redis integration tests in the main matrix

Install the redis extra and start a Redis 7 service so changed-line coverage includes the Redis clear code.

This branch has not been deployed

No deployments
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