FastAPI Simple Cache
FastAPI Simple Cache will cache responses from a decorated endpoint if the response
is JSON encodable or
a FastAPI Response.
Quick start
from fastapi import FastAPI, Request
app = FastAPI()
# Initialize in startup event
from fastapi_simple_cache import FastAPISimpleCache
from fastapi_simple_cache.backends.inmemory import InMemoryBackend
@app.on_event("startup")
async def startup():
backend = InMemoryBackend()
FastAPISimpleCache.init(backend=backend)
# Use the @cache decorator
from fastapi_simple_cache.decorator import cache
@app.get("/")
@cache(expire=3600) # Set expiration in seconds
def root(request: Request): # Add a Request typed parameter
return {"datetime": datetime.utcnow()}
Check here for FastAPI application examples with different backends and features.
Installation
The installation depends on the backend.
- In memory:
pip install fastapi-simple-cache - Redis:
pip install "fastapi-simple-cache[redis]" - Firestore:
pip install "fastapi-simple-cache[firestore]"
Backends
In memory
The InMemoryBackend class implements an in-memory backend.
from fastapi_simple_cache.backends.inmemory import InMemoryBackend
@app.on_event("startup")
async def startup():
backend = InMemoryBackend()
FastAPISimpleCache.init(backend=backend)
Redis
The RedisBackend class implements a Redis backend.
from redis.asyncio import ConnectionPool, client
from fastapi_simple_cache.backends.redis import RedisBackend
@app.on_event("startup")
async def startup():
pool = ConnectionPool.from_url(url="redis://localhost:6379")
backend = RedisBackend(redis=client.Redis(connection_pool=pool))
FastAPISimpleCache.init(backend=backend)
Firestore
The FirestoreBackend class implements a Google Firestore backend.
import firebase_admin
from firebase_admin import firestore, credentials
from fastapi_simple_cache.backends.firestore import FirestoreBackend
@app.on_event("startup")
async def startup():
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {"projectId": "gcp_project"})
db = firestore.client()
collection = db.collection("cache_collection")
backend = FirestoreBackend(collection=collection)
FastAPISimpleCache.init(backend=backend)
Features
Namespaces
You can add the parameter namespace on cache initialization to modify
the storage keys. Use this feature if you need to share same cache
environment with other applications but with different keys.
@app.on_event("startup")
async def startup():
backend = InMemoryBackend()
FastAPISimpleCache.init(
backend=backend,
namespace="my-app"
)
Multi backends
Use more than one backend to cache responses with the backend parameter
on cache initialization. This feature is useful if you want to check an
in-memory cache before an external cache.
from fastapi_simple_cache.backends.inmemory import InMemoryBackend
from fastapi_simple_cache.backends.redis import RedisBackend
@app.on_event("startup")
async def startup():
inmem_backend = InMemoryBackend()
redis_backend = RedisBackend(...)
FastAPISimpleCache.init(
backend=[inmem_backend, redis_backend]
)
Valid status codes
Set valid status codes to cache responses in the @cache parameter
status_codes (defaults to [200]).
@app.get("/")
@cache(expire=3600, status_codes=[200, 201])
def root(request: Request):
return {"datetime": datetime.utcnow()}
No cache
Avoid storing a request/response by adding the header
cache-control: no-cache to the request. This works both for the client
and the server.
License
FastAPI Fire Cache is released under the GNU General Public License v3.0 or later, see here for a description of this license, or see the LICENSE file for the full text.
Release files for fastapi-simple-cache 0.1.5
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-simple-cache-0.1.5.tar.gz | 23.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_simple_cache-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size:44.0 kB
Release files / fastapi-simple-cache-0.1.5.tar.gz
| Download URL | fastapi-simple-cache-0.1.5.tar.gz |
|---|---|
| Size | 23.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2b46a1cdf73192d42dd1646e9045ac226e3f13764e17b2417782b27194cb8dfc
|
|
BLAKE2b-256 checksum How to use checksums |
adc1adb5bc899cf3f3f62b6c85640a64c9531e2133a092b5140553f357cedf2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.1
|
Release files / fastapi_simple_cache-0.1.5-py3-none-any.whl
| Download URL | fastapi_simple_cache-0.1.5-py3-none-any.whl |
|---|---|
| Size | 20.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f64f54b1b16c69d3400625fc3e103da29c0b78324c760cdb86a59ae8f74b4b1c
|
|
BLAKE2b-256 checksum How to use checksums |
a9a67e370f19aba67065018068d7800751b9a62c2e996e0cd54c80d5c53b1f74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.1
|