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.1 (2026-07-06)

Bug Fixes

  • Ship py.typed marker for PEP 561 type information (#11, 1a7eb6f)

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.1.tar.gz (116.3 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.1-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_cache_di-0.2.1.tar.gz
  • Upload date:
  • Size: 116.3 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.1.tar.gz
Algorithm Hash digest
SHA256 c2fbea53b4f74e1a35f1bf6d9c50a7b8daa6324b0e2f82766e3ef961a890ae08
MD5 24bbb5a27b0bbe269e16433baede09c8
BLAKE2b-256 c3974fa769c0901a8290e72e8b058bef30c27ecdaca30ebd0d25dd58d25d1b07

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_cache_di-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 13eb84e91f87d14436791782875581b0ffe51b16999fe9356230f860b93c5912
MD5 8b49d6bf61640ae831d15dde5eb560e4
BLAKE2b-256 6af270058ca22f7dc8420dd0b286f89d99f0428224be2b08834486a6afdd3bc4

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