Repository navigation
feat(backends): cap MemoryBackend with max_entries and LRU eviction - #442
Merged
Merged
Conversation
…242) Without a limit the in-memory cache grows until entries expire, and a client can grow it on purpose by varying the query string of a cached route. `MemoryBackend(max_entries=...)` evicts the least recently used entry when a write would exceed the limit; a read hit and every write count as a use. The default stays unlimited. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UTXhK1BtwXTBDaouEcKtpW
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.
Fixes #242
Stack 1/4 (roadmap #425): this → #334 (
timedeltaTTLs) → #335 (X-Cache) → #248 (@cached). Merge this one first.Summary
MemoryBackend(max_entries=...)caps the number of entries. Storing one past the limit evicts the least recently used entry, expired or not; a read hit and every write (set,set_if_absent,set_if_equals,expire_if_equals,increment) count as a use. Monitoring reads (get_all_keys,get_cache_data) and rejected conditional writes do not.self.cachebecomes anOrderedDict(still adictfor callers); eviction ispopitem(last=False), O(1).max_entriesmust be a positiveintorNone; the default stays unlimited, so nothing changes for existing users. Bytes are not counted.BACKENDS.md(en + zh-TW) in-memory section;changelog.d/242.added.md.Tests:
tests/backends/test_memory.py(validation, LRU order, what counts as a use, replace-in-place,clear) andtests/test_cache_max_entries.py(a client varying?junk=on a@cache(ttl=3600)route cannot grow the cache past the limit).Checklist
tests/cover the changechangelog.d/<issue>.<section>.md(required for any change underfastapi_cachex/)uv run pre-commit run --all-filesanduv run pytestpass🤖 Generated with Claude Code
https://claude.ai/code/session_01UTXhK1BtwXTBDaouEcKtpW
Generated by Claude Code