FastAPI-Cache X
A high-performance caching extension for FastAPI, providing comprehensive HTTP caching support and optional session management.
Documentation: https://fastapi-cachex.readthedocs.io/en/latest/ — guides and the full API reference.
Features
- HTTP caching — a
@cachedecorator for GET routes withCache-Control,ETag/If-None-Match(304) and per-route invalidation. - Application cache —
CacheManagerfor caching arbitrary JSON values in your own code, with compute-on-missget_or_set(). - Backends — in-memory, Redis and Memcached, with atomic counters, one-shot values and locks.
- Sessions (optional) — HMAC-signed or JWT session tokens over headers, bearer tokens or cookies, with sliding expiration and IP/User-Agent binding.
- OAuth state — one-time state tokens for CSRF protection in OAuth/OIDC flows.
Installation
uv add fastapi-cachex
Everything in the core package works with the in-memory backend. The other backends and the optional session transports ship as extras:
| Extra | Install | Pulls in | Needed for |
|---|---|---|---|
redis |
uv add "fastapi-cachex[redis]" |
redis[hiredis], orjson |
AsyncRedisCacheBackend |
memcache |
uv add "fastapi-cachex[memcache]" |
pymemcache |
MemcachedBackend (note: memcache, not memcached) |
jwt |
uv add "fastapi-cachex[jwt]" |
PyJWT |
SessionConfig(token_format="jwt") |
Extras combine: uv add "fastapi-cachex[redis,jwt]".
Quick Start
from fastapi import FastAPI
from fastapi_cachex import AppCache, BackendProxy, cache
from fastapi_cachex.backends import MemoryBackend
app = FastAPI()
BackendProxy.set(MemoryBackend()) # or AsyncRedisCacheBackend / MemcachedBackend
@app.get("/items/{item_id}")
@cache(ttl=60) # served from the cache for 60 seconds, with ETag revalidation
async def read_item(item_id: int):
return {"item_id": item_id}
def build_report() -> dict:
return {"total": 42} # stands in for something slow
@app.get("/report")
async def report(cache: AppCache):
# Cache any JSON value in your own code.
return await cache.get_or_set("report", build_report, ttl=300)
Documentation
- HTTP caching — the
@cachedecorator, Cache-Control directives, cache keys, invalidation and monitoring routes - Cache flow — what happens inside a cached request
- Application cache —
CacheManager - Backends — choosing and configuring a backend, atomic primitives
- Session management and JWT claims
- OAuth state — one-shot OAuth/CSRF state tokens
- API reference
- Development guide and contributing
- Changelog · Known limitations and planned work
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Release files for fastapi-cachex 0.3.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_cachex-0.3.6.tar.gz | 49.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_cachex-0.3.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 113.1 kB
Release files / fastapi_cachex-0.3.6.tar.gz
| Download URL | fastapi_cachex-0.3.6.tar.gz |
|---|---|
| Size | 49.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
278f7247a8add290fe184c4e3ef41117155c08d41ef2a3e768b9db4bd263eb2b
|
|
BLAKE2b-256 checksum How to use checksums |
0bc9aa5694d8153839661114f982406a227889f957feffd7472e753b36d28745
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / fastapi_cachex-0.3.6-py3-none-any.whl
| Download URL | fastapi_cachex-0.3.6-py3-none-any.whl |
|---|---|
| Size | 63.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7ed98822268c3c802930422f7600eb6ec9ba0f40db00fc9f86213a55cf9353ac
|
|
BLAKE2b-256 checksum How to use checksums |
64e550d2a6bd815eb47ca5981b28e1995088a8fe878c129aab182439f328f6d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency log