FastAPI-Cache X
A high-performance caching extension for FastAPI: a server-side response cache with Cache-Control and ETag support, and application-level caching.
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()and atomic store-if-absentadd(). - Backends — in-memory, Redis and Memcached, with atomic counters, one-shot values and locks.
- Sessions and OAuth state (deprecated): signed session tokens and one-time OAuth state tokens. Both are deprecated in 0.4.0 and removed in 0.5.0; see where to move.
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 |
memcached |
uv add "fastapi-cachex[memcached]" |
pymemcache |
MemcachedBackend |
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. Concurrent misses run
# build_report once: get_or_set() locks by default.
return await cache.get_or_set("report", build_report, ttl=300)
Documentation
- Migrating to 0.4.0 — what 0.4.0 changes and how to upgrade from 0.3.x
- HTTP caching — the
@cachedecorator, Cache-Control directives, cache keys, invalidation and monitoring routes - Application cache —
CacheManager - Backends — choosing and configuring a backend, atomic primitives
- Distributed lock —
CacheLockfor multi-process mutual exclusion - Deprecated, removed in 0.5.0: Session management, OAuth state (one-shot OAuth/CSRF state tokens) and JWT claims
- Cache flow — what happens inside a cached request
- Runnable examples — one complete app per feature, each covered by the test suite
- API reference
- Development guide and contributing · Security policy — report vulnerabilities privately, not in public issues
- Changelog · Known limitations and planned work
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Metadata
Release files for fastapi-cachex 0.4.1
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.4.1.tar.gz | 105.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_cachex-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 226.9 kB
Release files / fastapi_cachex-0.4.1.tar.gz
| Download URL | fastapi_cachex-0.4.1.tar.gz |
|---|---|
| Size | 105.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c013414e84a8fe52d18c3402ad1d8971efbeb78219fb1ff61b5c3533c4ebf2d6
|
|
BLAKE2b-256 checksum How to use checksums |
798ac7aa5dcefae623d83b5d8f992c641039414d77c529e7d843e0dc12efe76b
|
| 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 Oct 3, 2026.
Transparency logRelease files / fastapi_cachex-0.4.1-py3-none-any.whl
| Download URL | fastapi_cachex-0.4.1-py3-none-any.whl |
|---|---|
| Size | 121.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
48987dd93872ee4ad0c5bebf1bbc73fdd0213c88b0076701ad739c7045238743
|
|
BLAKE2b-256 checksum How to use checksums |
1891ceec8f11fe63585d5432a406a4f615a0686ea43b37d516264518e3188875
|
| 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 Oct 3, 2026.
Transparency log