Skip to main content

Speed up FastAPI startup by caching dependency-tree introspection

Project description

fastapi-cache-di

Latest Version MIT License Build Status Codecov semantic-release

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_cache_di-0.2.0.tar.gz (116.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_cache_di-0.2.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_cache_di-0.2.0.tar.gz.

File metadata

  • Download URL: fastapi_cache_di-0.2.0.tar.gz
  • Upload date:
  • Size: 116.1 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for fastapi_cache_di-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9953c16c464dc7af13e2a4170708ac239f94a8e927cfe2d1cd3ba2ab3384abb9
MD5 e35c8382b410480e788030955b4b9980
BLAKE2b-256 adadc11101363f9f8d2b3168fb464d664448f4de69313476a97b1fa31963a90b

See more details on using hashes here.

File details

Details for the file fastapi_cache_di-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_cache_di-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for fastapi_cache_di-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b53713e7f681e456cfc4defa1951ee290cdaa4e2d3b37a27d0cc4056a073426a
MD5 c10d5a87e7fe1a39ea970866de55bc6c
BLAKE2b-256 e7e454b39f9cd45f2c8c43f86f49a5c63e7071b7e8b7a9c642ee5cdff97069ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page