Speed up FastAPI startup by caching its dependency-tree introspection.
At startup, FastAPI re-introspects every route's dependency tree with no caching,
so dependencies shared across many routes (auth checks, DB handles, role guards …)
are parsed from scratch again and again. fastapi-cache-di memoizes that work for
the duration of route loading, turning O(routes × tree-depth) into
O(routes + unique-deps), then restores FastAPI's originals.
pip install fastapi-cache-di
# or
uv add fastapi-cache-diWrap the code that loads your routes with the context manager:
from fastapi import APIRouter, FastAPI
from fastapi_cache_di import fastapi_deps_cache
app = FastAPI()
users = APIRouter()
orders = APIRouter()
billing = APIRouter()
with fastapi_deps_cache():
# Registering routes introspects each dependency tree; caching is active here.
app.include_router(users)
app.include_router(orders)
app.include_router(billing)
# originals restored here; cache memory freedManual patch/unpatch, sharing and inspecting the cache, and the cache-key details are covered in the documentation.
Full documentation is available at toilal.github.io/fastapi-cache-di (usage guide, internals, and API reference).
- Python ≥ 3.12
- FastAPI ≥ 0.112.4
MIT © Rémi Alvergnat