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, application-level caching, and optional session management.

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

Features

  • HTTP caching — a @cache decorator for GET routes with Cache-Control, ETag / If-None-Match (304) and per-route invalidation.
  • Application cache — CacheManager for caching arbitrary JSON values in your own code, with compute-on-miss get_or_set() and atomic store-if-absent add().
  • Backends — in-memory, Redis and Memcached, with atomic counters, one-shot values and locks.
  • Sessions (optional) — HMAC-signed or JWT session tokens over headers, bearer tokens or cookies, with sliding expiration and IP/User-Agent binding.
  • OAuth state — one-time state tokens for CSRF protection in OAuth/OIDC flows.

Installation

uv add fastapi-cachex

Everything in the core package works with the in-memory backend. The other backends and the optional session transports 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 (the older memcache name still works until 0.4.0)
jwt uv add "fastapi-cachex[jwt]" PyJWT SessionConfig(token_format="jwt")

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

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(cache: AppCache):
    # Cache any JSON value in your own code. lock=True runs build_report once
    # for concurrent misses (the default from 0.4.0).
    return await cache.get_or_set("report", build_report, ttl=300, lock=True)

Documentation

License

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

Metadata

Release files for fastapi-cachex 0.3.9

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.3.9
File Size Uploaded
fastapi_cachex-0.3.9.tar.gz 95.6 kB Details

Built distribution (wheel)

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

Total release size: 205.6 kB

Release files / fastapi_cachex-0.3.9.tar.gz

Download URL fastapi_cachex-0.3.9.tar.gz
Size 95.6 kB
Tags Source
SHA-256 checksum
How to use checksums
955ab23b20c0e6dc3413aa3ee4d80b34c6d472ec05b4fed3a668754a98af83c2
BLAKE2b-256 checksum
How to use checksums
c2b307e79a5d20213b2b5727e6d53604ed886d30aee92d3157078d422b782ca2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 29, 2026.

Transparency log

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

Download URL fastapi_cachex-0.3.9-py3-none-any.whl
Size 110.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2022f30bb2cb91868db14a5522fb7f80aa248669a2bfca0cebccc6933158638d
BLAKE2b-256 checksum
How to use checksums
1a8c26f1955ed4a72c4ba270ef200be86dda2c03230ab0ba4b12a36812b806ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 29, 2026.

Transparency log

Release history Release notifications | RSS feed

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

This release

0.3.9 This release

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.12

2 release files

0.2.11

2 release files

0.2.10

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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