Skip to main content

FastAPI-Cache X

uv Ruff Tests Coverage Status

Downloads Weekly downloads Monthly downloads

PyPI version Python Versions

English | 繁體中文

A high-performance caching extension for FastAPI: a server-side response cache with Cache-Control and ETag support, and application-level caching.

Documentation: https://fastapi-cachex.readthedocs.io/en/stable/ — guides and the full API reference.

Features

  • HTTP caching — a @cache decorator for GET routes with Cache-Control, ETag / If-None-Match (304), invalidation by route or by tag, and optional serving of a stale response while it is refreshed or when the handler fails.
  • Application cache — CacheManager for caching arbitrary JSON values in your own code, with compute-on-miss get_or_set(), atomic store-if-absent add(), batch reads and writes, typed values through pydantic, and @cached for a plain function, keyed on its arguments.
  • Backends — in-memory, Redis and Memcached, with atomic counters, one-shot values and locks. Tags work on all three.
  • Observability — an event hook that reports hits, misses, stores and backend errors to your own metrics, and monitoring routes.

Installation

uv add fastapi-cachex

Everything in the core package works with the in-memory backend. The other backends ship as extras:

Extra Install Pulls in Needed for
redis uv add "fastapi-cachex[redis]" redis[hiredis], orjson AsyncRedisCacheBackend
memcached uv add "fastapi-cachex[memcached]" pymemcache MemcachedBackend

Extras combine: uv add "fastapi-cachex[redis,memcached]".

Quick Start

from fastapi import FastAPI

from fastapi_cachex import AppCache, BackendProxy, cache
from fastapi_cachex.backends import MemoryBackend

app = FastAPI()
BackendProxy.set(MemoryBackend())  # or AsyncRedisCacheBackend / MemcachedBackend


@app.get("/items/{item_id}")
@cache(ttl=60)  # served from the cache for 60 seconds, with ETag revalidation
async def read_item(item_id: int):
    return {"item_id": item_id}


def build_report() -> dict:
    return {"total": 42}  # stands in for something slow


@app.get("/report")
async def report(app_cache: AppCache):
    # Cache any JSON value in your own code. Concurrent misses run
    # build_report once: get_or_set() locks by default.
    return await app_cache.get_or_set("report", build_report, ttl=300)

Documentation

The guides are built from docs/en/ (English) and docs/zh-TW/ (繁體中文) into the site linked below. Read them there: a code block that includes one of the runnable examples renders on the site, while GitHub shows only its --8<-- include line (the sentence before each block links the example file).

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Metadata

Release files for fastapi-cachex 0.7.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-cachex 0.7.2
File Size Uploaded Provenance Actions
fastapi_cachex-0.7.2.tar.gz 137.5 kB With provenance Details for fastapi_cachex-0.7.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-cachex 0.7.2
File Interpreter ABI Platform Actions
fastapi_cachex-0.7.2-py3-none-any.whl Python 3 none any Details for fastapi_cachex-0.7.2-py3-none-any.whl

Total release size: 289.5 kB

Release files / fastapi_cachex-0.7.2.tar.gz

Download URL fastapi_cachex-0.7.2.tar.gz
Size 137.5 kB
Tags Source
SHA-256 checksum
How to use checksums
639958cc8c169fb070a4a58440d84160d35e0bf5ec338c8a68b202179fdbad25
BLAKE2b-256 checksum
How to use checksums
d0662638a0a437e223e77388ef0a365a5632402014c3f351e2131e83f0ed1a0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Release files / fastapi_cachex-0.7.2-py3-none-any.whl

Download URL fastapi_cachex-0.7.2-py3-none-any.whl
Size 152.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
609ddf6189baa09d5a812b9fe9014d05f8839ed9c12fac7fb0217f5817ad494f
BLAKE2b-256 checksum
How to use checksums
5bda37df47c1de9403d2ed6ab703f687745784948c6482f393a24b2be021ac38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Security for fastapi-cachex 0.7.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastapi_cachex-0.7.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.7.2 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page