Dead-simple caching for FastAPI — one decorator, done
Project description
fastapi-cache-pro
Dead-simple caching for FastAPI — one decorator, done
Install
pip install fastapi-cache-pro
Usage
from fastapi import FastAPI, Request
from fastapi_cache_pro import setup_cache, cache
app = FastAPI()
setup_cache() # uses in-memory by default
@app.get("/users")
@cache(ttl=300) # cache for 5 minutes
async def get_users(request: Request):
return await db.get_all_users() # only called once per 5 min
That's it. Second request gets cached response with X-Cache: HIT header.
Redis Backend
import redis.asyncio as redis
from fastapi_cache_pro import setup_cache, RedisBackend
r = redis.from_url("redis://localhost:6379")
setup_cache(RedisBackend(r))
Features
| Feature | Description |
|---|---|
@cache(ttl=60) |
Cache any GET endpoint |
| Memory backend | Zero-config, works out of the box |
| Redis backend | For multi-instance deployments |
| Query params | Different params = different cache entries |
X-Cache header |
Response tells you HIT or MISS |
| Custom prefix | Namespace your cache keys |
| Pluggable | Write your own backend (Memcached, DynamoDB, etc.) |
API
# Setup
setup_cache() # memory (default)
setup_cache(RedisBackend(redis_client)) # redis
# Decorator
@cache(ttl=60) # 60 seconds
@cache(ttl=3600) # 1 hour
@cache(ttl=300, prefix="api") # custom prefix
How It Works
- Request comes in → decorator builds cache key from path + query params
- Key found → return cached JSON, set
X-Cache: HIT - Key not found → call your function, cache result, set
X-Cache: MISS - TTL expires → next request refreshes the cache
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_cache_pro-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_cache_pro-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13ad2ecd06a31d54b909e1fb2a8b8d95acfac42d55fb4bad503cced0c2cbabe6
|
|
| MD5 |
a6e90a8170de61e7aeba4190b3ac5bd4
|
|
| BLAKE2b-256 |
3c87c2fd0db874f4cd5e892717cb5af17b7e5396d88d7a8813ae5f06df1625c8
|
File details
Details for the file fastapi_cache_pro-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_cache_pro-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f930cabae007ae3365337c3f02a3b2c515005b4c7266b814d1b32ae787b974f
|
|
| MD5 |
f1a74f3bd58e07d6465976ebceec9253
|
|
| BLAKE2b-256 |
40f466d5879e6cd6370393e0ea30f6b22f8651bb61997643895a1f9689751403
|