skillfed

fastapi-cache2

Cache for FastAPI

fastapi-cache2 v0.2.2 1.1M downloads/30d#4,443 on PyPI1,866
Permissive license Apache-2.0 AGING released

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-cache2

uv

uv add fastapi-cache2

poetry

poetry add fastapi-cache2

Installing 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

License :: OSI Approved :: Apache Software LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

fastapi caching decoratorcache fastapi responseshttp cache headers etagmemcached backend fastapidynamodb cache layerresponse caching fastapicache control headers
cachingmulti-backendhttp-headers

More Dynamic Content packages