fastapi-cache-di
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.
Install
pip install fastapi-cache-di
# or
uv add fastapi-cache-di
Usage
Wrap 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 freed
Manual patch/unpatch, sharing and inspecting the cache, and the cache-key details are covered in the documentation.
Documentation
Full documentation is available at toilal.github.io/fastapi-cache-di (usage guide, internals, and API reference).
Requirements
- Python ≥ 3.12
- FastAPI ≥ 0.112.4
License
MIT © Rémi Alvergnat
CHANGELOG
v0.2.1 (2026-07-06)
Bug Fixes
v0.2.0 (2026-07-05)
Features
v0.1.0 (2026-07-05)
- Initial Release
Changelog entries are generated automatically by python-semantic-release from Conventional Commits on release.
Metadata
Release files for fastapi-cache-di 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_cache_di-0.2.1.tar.gz | 116.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_cache_di-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 127.1 kB
Release files / fastapi_cache_di-0.2.1.tar.gz
| Download URL | fastapi_cache_di-0.2.1.tar.gz |
|---|---|
| Size | 116.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c2fbea53b4f74e1a35f1bf6d9c50a7b8daa6324b0e2f82766e3ef961a890ae08
|
|
BLAKE2b-256 checksum How to use checksums |
c3974fa769c0901a8290e72e8b058bef30c27ecdaca30ebd0d25dd58d25d1b07
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / fastapi_cache_di-0.2.1-py3-none-any.whl
| Download URL | fastapi_cache_di-0.2.1-py3-none-any.whl |
|---|---|
| Size | 10.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
13eb84e91f87d14436791782875581b0ffe51b16999fe9356230f860b93c5912
|
|
BLAKE2b-256 checksum How to use checksums |
6af270058ca22f7dc8420dd0b286f89d99f0428224be2b08834486a6afdd3bc4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|