fastapi-cache2
Cache for FastAPI
What it is and what it does
fastapi-cache2 is a caching layer for FastAPI applications that transparently stores and retrieves endpoint and function results. It wraps your route handlers with a @cache decorator that automatically manages cache expiration, key generation, and backend storage. The package supports multiple backends—Redis, Memcached, DynamoDB, and in-memory—so you can choose the right storage for your deployment.
The decorator integrates with FastAPI's dependency injection to handle HTTP cache semantics: it can set ETag and Cache-Control headers, return 304 Not Modified responses for conditional requests, and track cache hits and misses. You can customize cache behavior per endpoint with parameters like expiration time, namespace, and key-building strategy, or implement custom coders to handle non-JSON data types like Pydantic models and dataclasses.
Use it for:
- Cache API responses with automatic expiration to reduce database queries and improve endpoint latency.
- Store results of expensive computations across multiple application instances using a shared backend.
- Serve cached responses with proper HTTP cache headers and conditional request support for browser caching.
- Cache function results in memory for single-instance deployments or testing without external dependencies.
- Implement custom cache keys based on request method, URL, and query parameters instead of function arguments.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Caches FastAPI endpoint and function results with support for Redis, Memcached, DynamoDB, and in-memory backends, including HTTP cache header handling.
Yes, with conditions. The package is straightforward to integrate and widely used, but its aging maintenance status (751 days since last release) means you should verify compatibility with your FastAPI and Python versions before adopting it. No known vulnerabilities. Choose this if you need transparent endpoint caching with multiple backend options; avoid if you require active maintenance or cutting-edge feature updates.
Install
fastapi-cache2 on PyPI
pip
pip install fastapi-cache2uv
uv add fastapi-cache2poetry
poetry add fastapi-cache2Installing fastapi-cache2
Before you install
Low install friction with five runtime dependencies. Maintenance status is aging—last release was 2024-07-24 and the repository has not been updated for 751 days, though it remains active and unarchived with 1866 stars.
License in practice
Licensed under Apache-2.0 (permissive), allowing commercial use, modification, and distribution with minimal restrictions.
Quickstart
pip install fastapi-cache2
from fastapi_cache import FastAPICache
from fastapi_cache.decorator import cache
FastAPICache.init(backend, prefix="fastapi-cache")
@app.get("/")
@cache(expire=60)
async def index():
return dict(hello="world")
Requires a running cache backend (Redis, Memcached, or DynamoDB) or uses in-memory storage; backend client configuration depends on chosen backend.
Verify before relying
- Whether the aging maintenance status (751 days since last release) affects compatibility with recent FastAPI or Python versions.
- Performance characteristics and memory overhead of the in-memory backend for production workloads.
- Specific backend setup requirements and client library versions for Redis, Memcached, and DynamoDB.
Package facts
| License | Apache-2.0 (permissive) |
| Python support | supports the current Python release (<4.0,>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 5 — fastapi, importlib-metadata, pendulum, typing-extensions, uvicorn |
| Maintenance | aging — 751 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,050,069/month — #4,443 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: fastapi_cache2-0.2.2-py3-none-any.whl
Keywords: fastapi, cache, caching
Tags
More Dynamic Content packages
MarkupSafe provides a text object that escapes…
permissive · top 100 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
soupsieveSoupsieve is a CSS selector library designed to…
permissive · top 100 on PyPI
WerkzeugWerkzeug is a WSGI utility library providing…
permissive · top 1,000 on PyPI
FlaskFlask is a lightweight WSGI web application…
permissive · top 1,000 on PyPI
MakoMako compiles Python-embedded templates into…
permissive · top 1,000 on PyPI
cachelibCacheLib provides a unified API for interacting…
permissive · top 5,000 on PyPI
cashewsAsync cache framework with decorator and direct…
permissive · top 15,000 on PyPI
aiohttp-client-cacheAdds transparent request caching to…
permissive · top 15,000 on PyPI
django-cache-urlConfigures Django cache backends from…
permissive · top 15,000 on PyPI
requests-cacheAdds persistent HTTP caching to the requests…
permissive · top 5,000 on PyPI
fastapi-lifespan-managerProvides a lifespan manager for FastAPI that…
permissive · top 15,000 on PyPI
fastapi-utilitiesProvides reusable utilities for FastAPI…
permissive · top 15,000 on PyPI
aiocacheProvides asyncio-compatible caching with…
unclear · top 5,000 on PyPI
fastapi-decoratorsConverts functions into FastAPI-compatible…
permissive · top 15,000 on PyPI
fastapi-versioningAdds API versioning to FastAPI applications by…
permissive · top 15,000 on PyPI