The official Redis integration with FastAPI
Project description
Official FastAPI integration for Redis
Idiomatic Redis integration for FastAPI - connection management and DI-based caching with automatic key consistency.
Features
- Fluent setup —
FastAPIRedis(app).lifespan().caching()configures pools and caching in one chain, attaching to the FastAPI lifespan events - Dependency injection —
cache(),cache_evict(),cache_put()asDepends()factories, plusCacheBackendfor complex invalidation and conditional logic - HTTP-native caching —
ETag,304 Not Modified,Cache-Controldirectives out of the box - Testable — full
dependency_overridessupport; no need for monkey-patching - Pydantic-validated configuration — fully configurable via environment variables or via an
.envfile
Requirements
| Dependency | Supported versions |
|---|---|
| Python | 3.10 to 3.14 |
| FastAPI | 0.115+ |
| redis-py | 6.0+ |
| Pydantic | 2.0+ |
| Redis server | 7.4+ |
Installation
pip install fastapi-redis-sdk
Quick Start
from fastapi import FastAPI
from redis_fastapi import FastAPIRedis, RedisDep, AsyncRedisDep
app = FastAPI()
FastAPIRedis(app).lifespan()
@app.get("/items")
def get_items(redis: RedisDep):
return {"items": redis.get("items")}
@app.get("/async-items")
async def get_items_async(redis: AsyncRedisDep):
return {"items": await redis.get("items")}
Connection pools are managed automatically and closed on shutdown. The builder wraps any existing lifespan - multiple libraries can each register their own without conflicting. See Configuration for connection options.
Caching
Two caching patterns for different needs, sharing the same connection pool:
| Pattern | Best for |
|---|---|
cache(), cache_evict(), cache_put() |
Most endpoints - read/write/invalidate |
| CacheBackend | Complex invalidation, conditional logic |
DI factories - read, invalidate, write-through
from fastapi import Depends
from redis_fastapi import FastAPIRedis, cache, cache_evict, cache_put, default_key_builder
app = FastAPI()
FastAPIRedis(app).lifespan().caching()
# READ - cache GET responses
@app.get("/products/{product_id}", dependencies=[Depends(cache(ttl=300, eviction_group="products"))])
async def get_product(product_id: int):
return await db.get_product(product_id)
# INVALIDATE - evict the cached entry on delete
@app.delete(
"/products/{product_id}",
dependencies=[Depends(cache_evict(eviction_group="products", key_builder=default_key_builder))],
)
async def delete_product(product_id: int):
await db.delete(product_id)
# WRITE-THROUGH - update the cache so the next GET is a HIT
@app.put(
"/products/{product_id}",
dependencies=[Depends(cache_put(eviction_group="products", key_builder=default_key_builder, ttl=300))],
)
async def replace_product(product_id: int, body: Product):
return await db.update(product_id, body)
All three factories share the same key_builder, so GET, DELETE, and PUT on the same path target the exact same cache key. Responses include X-Redis-Cache (HIT/MISS), Cache-Control, and ETag headers with 304 Not Modified support. Full dependency_overrides support makes testing straightforward - no monkey-patching required.
CacheBackend - full control via dependency injection
from redis_fastapi import CacheBackendDep
@app.get("/dashboard/{user_id}")
async def dashboard(user_id: int, cache: CacheBackendDep):
cached = await cache.get(f"stats:{user_id}", eviction_group="dashboard")
if cached:
return cached
result = await compute_dashboard(user_id)
await cache.set(f"stats:{user_id}", result, ttl=300, eviction_group="dashboard")
return result
Provides get/set/delete/has/delete_group with automatic JSON serialization. Use it for conditional caching, cascade invalidation, dynamic TTL, and intermediate result caching.
See the Caching Guide for detailed examples, feature comparison, and best practices.
Configuration
All settings are read from environment variables (prefixed REDIS_) or a .env file. Set REDIS_URL for the simplest setup:
export REDIS_URL=redis://user:pass@host:6379/0
Or configure individual fields:
export REDIS_HOST=redis.example.com
export REDIS_PORT=6380
export REDIS_PASSWORD=secret
Additional options: TLS (REDIS_SSL, REDIS_SSL_CERTFILE, etc.), connection pool (REDIS_MAX_CONNECTIONS, REDIS_SOCKET_TIMEOUT), OSS Cluster mode (REDIS_CLUSTER=true), key prefix (REDIS_PREFIX), and default cache TTL (REDIS_DEFAULT_TTL, default 0 = no expiry).
For programmatic configuration:
from redis_fastapi import get_settings
settings = get_settings()
settings.url = "redis://custom:6379/0"
settings.default_ttl = 120
See the Configuration Guide for the full environment variable reference and API details.
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_redis_sdk-0.7.0.tar.gz.
File metadata
- Download URL: fastapi_redis_sdk-0.7.0.tar.gz
- Upload date:
- Size: 23.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01bd948430dc97a1f4a3674fcbbf8316ce8856d1ee4dbc1fe669c7decca98084
|
|
| MD5 |
3beb12833bf6f73b8cae9367c2c0d9dc
|
|
| BLAKE2b-256 |
903522064b1b7a3160a64e17498b823af3cae86f4e35c20274e719367861ccef
|
Provenance
The following attestation bundles were made for fastapi_redis_sdk-0.7.0.tar.gz:
Publisher:
release.yml on redis/fastapi-redis-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_redis_sdk-0.7.0.tar.gz -
Subject digest:
01bd948430dc97a1f4a3674fcbbf8316ce8856d1ee4dbc1fe669c7decca98084 - Sigstore transparency entry: 1911894438
- Sigstore integration time:
-
Permalink:
redis/fastapi-redis-sdk@c6945c2f9cac34bfc011b81a2a849a32b9142f69 -
Branch / Tag:
refs/tags/0.7.0 - Owner: https://github.com/redis
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c6945c2f9cac34bfc011b81a2a849a32b9142f69 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_redis_sdk-0.7.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_redis_sdk-0.7.0-py3-none-any.whl
- Upload date:
- Size: 26.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07a685664900ddb72882d351ebc6b712d4aa9ba42b131e2010f47cc48785f4a1
|
|
| MD5 |
14f06e83b8d45a786ef227b60b738987
|
|
| BLAKE2b-256 |
3b242c8c44ed7822090378295e03f9bd05f56c330ac29c847b4d38ea8be1c30e
|
Provenance
The following attestation bundles were made for fastapi_redis_sdk-0.7.0-py3-none-any.whl:
Publisher:
release.yml on redis/fastapi-redis-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_redis_sdk-0.7.0-py3-none-any.whl -
Subject digest:
07a685664900ddb72882d351ebc6b712d4aa9ba42b131e2010f47cc48785f4a1 - Sigstore transparency entry: 1911894596
- Sigstore integration time:
-
Permalink:
redis/fastapi-redis-sdk@c6945c2f9cac34bfc011b81a2a849a32b9142f69 -
Branch / Tag:
refs/tags/0.7.0 - Owner: https://github.com/redis
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c6945c2f9cac34bfc011b81a2a849a32b9142f69 -
Trigger Event:
release
-
Statement type: