Skip to main content

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

  1. Request comes in → decorator builds cache key from path + query params
  2. Key found → return cached JSON, set X-Cache: HIT
  3. Key not found → call your function, cache result, set X-Cache: MISS
  4. TTL expires → next request refreshes the cache

License

MIT

Release files for fastapi-cache-pro 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-cache-pro 0.1.0
File Size Uploaded
fastapi_cache_pro-0.1.0.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-cache-pro 0.1.0
File Interpreter ABI Platform
fastapi_cache_pro-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / fastapi_cache_pro-0.1.0.tar.gz

Download URL fastapi_cache_pro-0.1.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
13ad2ecd06a31d54b909e1fb2a8b8d95acfac42d55fb4bad503cced0c2cbabe6
BLAKE2b-256 checksum
How to use checksums
3c87c2fd0db874f4cd5e892717cb5af17b7e5396d88d7a8813ae5f06df1625c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / fastapi_cache_pro-0.1.0-py3-none-any.whl

Download URL fastapi_cache_pro-0.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f930cabae007ae3365337c3f02a3b2c515005b4c7266b814d1b32ae787b974f
BLAKE2b-256 checksum
How to use checksums
40f466d5879e6cd6370393e0ea30f6b22f8651bb61997643895a1f9689751403
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page