Repository navigation
feat: @cached decorator for plain functions - #445
Merged
Merged
Conversation
`@cached(ttl=..., key=..., manager=None)` caches what an async or sync function returns through `CacheManager.get_or_set()`, so it gets the manager's prefix, JSON round-trip and stampede protection. The default key is `module.qualname:` plus a SHA-256 of the JSON-serialized arguments bound to the signature; `key=` takes a format template or a callable for methods and non-JSON arguments. The decorated function has `cache_key()` and `invalidate()`, and binds as a method. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UTXhK1BtwXTBDaouEcKtpW
Base automatically changed from
claude/lucid-archimedes-4gzb2o-335-x-cache
to
master
October 7, 2026 11:00
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 #248
Stack 4/4 (roadmap #425): #242 (
max_entries) → #334 (timedeltaTTLs) → #335 (X-Cache) → this. Based on the #335 branch; merge last. Only the last commit is this PR's.Summary
fastapi_cachex/cached.py, exported asfastapi_cachex.cachedandCachedFunction.@cached(ttl=..., key=..., manager=None, lock=None)on anasyncor sync function stores its result throughCacheManager.get_or_set(), so it gets the manager's prefix, JSON round-trip and stampede protection. The decorated function is always awaited; a sync function runs on the loop like aget_or_setfactory.module.qualname:+ SHA-256 of the arguments bound to the signature with defaults applied and serialized as canonical JSON, soload(1),load(user_id=1)andload(1, locale="en")share one entry. A non-JSON argument raisesCacheXErrornamingkey=rather than silently missing every time.key=is astr.formattemplate over the bound arguments ("user:{user_id}"; a plain string is a fixed key) or a callable given the call's arguments.manager=Noneresolves the application's manager (get_app_cache()) on each call, so one registered at startup is picked up.fn.cache_key(*args, **kwargs)andawait fn.invalidate(*args, **kwargs).CachedFunction.__get__bindsself, so methods,obj.load.cache_key(1)andobj.load.invalidate(1)work (withkey=, sinceselfis not JSON).ParamSpecand overloads:await load(1)is inferred as the function's return type, and a wrong argument type is a mypy error (checked with a probe file).APP_CACHE.md(en + zh-TW) driven by a new--8<--snippet inexamples/app_cache.py, API reference, README feature line;changelog.d/248.added.md.Tests:
tests/test_cached.py(25 tests: keys, templates, callables, invalidate, TTL and manager defaults, JSON round-trip, app-manager resolution, concurrent misses run once,lock=False, method binding, bad arguments) and the extendedtest_examples.py::test_app_cache.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