A caching library for FastAPI with support for Cache-Control, ETag, and multiple backends.
Project description
FastAPI-Cache X
A high-performance caching extension for FastAPI, providing comprehensive HTTP caching support.
Features
- Support for HTTP caching headers
Cache-ControlETagIf-None-Match
- Multiple backend cache support
- Redis
- Memcached
- In-memory cache
- Complete Cache-Control directive implementation
- Easy-to-use
@cachedecorator
Cache-Control Directives
| Directive | Supported | Description |
|---|---|---|
max-age |
:white_check_mark: | Specifies the maximum amount of time a resource is considered fresh. |
s-maxage |
:x: | Specifies the maximum amount of time a resource is considered fresh for shared caches. |
no-cache |
:white_check_mark: | Forces caches to submit the request to the origin server for validation before releasing a cached copy. |
no-store |
:white_check_mark: | Instructs caches not to store any part of the request or response. |
no-transform |
:x: | Instructs caches not to transform the response content. |
must-revalidate |
:white_check_mark: | Forces caches to revalidate the response with the origin server after it becomes stale. |
proxy-revalidate |
:x: | Similar to must-revalidate, but only for shared caches. |
must-understand |
:x: | Indicates that the recipient must understand the directive or treat it as an error. |
private |
:white_check_mark: | Indicates that the response is intended for a single user and should not be stored by shared caches. |
public |
:white_check_mark: | Indicates that the response may be cached by any cache, even if it is normally non-cacheable. |
immutable |
:white_check_mark: | Indicates that the response body will not change over time, allowing for longer caching. |
stale-while-revalidate |
:white_check_mark: | Indicates that a cache can serve a stale response while it revalidates the response in the background. |
stale-if-error |
:white_check_mark: | Indicates that a cache can serve a stale response if the origin server is unavailable. |
Installation
Using pip
pip install fastapi-cachex
Using uv (recommended)
uv pip install fastapi-cachex
Quick Start
from fastapi import FastAPI
from fastapi_cachex import cache
from fastapi_cachex import CacheBackend
app = FastAPI()
@app.get("/")
@cache(ttl=60) # Cache for 60 seconds
async def read_root():
return {"Hello": "World"}
@app.get("/no-cache")
@cache(no_cache=True) # Mark this endpoint as non-cacheable
async def non_cache_endpoint():
return {"Hello": "World"}
@app.get("/no-store")
@cache(no_store=True) # Mark this endpoint as non-cacheable
async def non_store_endpoint():
return {"Hello": "World"}
@app.get("/clear_cache")
async def remove_cache(cache: CacheBackend):
await cache.clear_path("/path/to/clear") # Clear cache for a specific path
await cache.clear_pattern("/path/to/clear/*") # Clear cache for a specific pattern
Backend Configuration
FastAPI-CacheX supports multiple caching backends. You can easily switch between them using the BackendProxy.
Cache Key Format
Cache keys are generated in the following format to avoid collisions:
{method}:{host}:{path}:{query_params}
This ensures that:
- Different HTTP methods (GET, POST, etc.) don't share cache
- Different hosts don't share cache (useful for multi-tenant scenarios)
- Different query parameters get separate cache entries
- The same endpoint with different parameters can be cached independently
All backends automatically namespace keys with a prefix (e.g., fastapi_cachex:) to avoid conflicts with other applications.
Cache Hit Behavior
When a cached entry is valid (within TTL):
- Default behavior: Returns the cached content with HTTP 200 status code directly without re-executing the endpoint handler
- With
If-None-Matchheader: Returns HTTP 304 Not Modified if the ETag matches - With
no-cachedirective: Forces revalidation with fresh content before deciding on 304
This means cached hits are extremely fast - the endpoint handler function is never executed.
In-Memory Cache (default)
If you don't specify a backend, FastAPI-CacheX will use the in-memory cache by default. This is suitable for development and testing purposes. The backend automatically runs a cleanup task to remove expired entries every 60 seconds.
from fastapi_cachex.backends import MemoryBackend
from fastapi_cachex import BackendProxy
backend = MemoryBackend()
BackendProxy.set_backend(backend)
Note: In-memory cache is not suitable for production with multiple processes. Each process maintains its own separate cache.
Memcached
from fastapi_cachex.backends import MemcachedBackend
from fastapi_cachex import BackendProxy
backend = MemcachedBackend(servers=["localhost:11211"])
BackendProxy.set_backend(backend)
Limitations:
- Pattern-based key clearing (
clear_pattern) is not supported by the Memcached protocol - Keys are namespaced with
fastapi_cachex:prefix to avoid conflicts - Consider using Redis backend if you need pattern-based cache clearing
Redis
from fastapi_cachex.backends import AsyncRedisCacheBackend
from fastapi_cachex import BackendProxy
backend = AsyncRedisCacheBackend(host="127.0.0.1", port=6379, db=0)
BackendProxy.set_backend(backend)
Features:
- Fully async implementation
- Supports pattern-based key clearing
- Uses SCAN instead of KEYS for safe production use (non-blocking)
- Namespaced with
fastapi_cachex:prefix by default - Optional custom key prefix for multi-tenant scenarios
Example with custom prefix:
backend = AsyncRedisCacheBackend(
host="127.0.0.1",
port=6379,
key_prefix="myapp:cache:",
)
BackendProxy.set_backend(backend)
Performance Considerations
Cache Hit Performance
When a cache hit occurs (within TTL), the response is returned directly without executing your endpoint handler. This is extremely fast:
@app.get("/expensive")
@cache(ttl=3600) # Cache for 1 hour
async def expensive_operation():
# This is ONLY executed when cache misses
# On cache hits, this function is never called
result = perform_expensive_calculation()
return result
Backend Selection
- MemoryBackend: Fastest for single-process development; not suitable for production
- Memcached: Good for distributed systems; has limitations on pattern clearing
- Redis: Best for production; fully async, supports all features, non-blocking operations
Documentation
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_cachex-0.2.0.tar.gz.
File metadata
- Download URL: fastapi_cachex-0.2.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73b593a82f46f35fc9a63f340d8a914c581e27576da9ff42545c7afdd296b617
|
|
| MD5 |
75ae5db2dd647f535f73c76c20d85745
|
|
| BLAKE2b-256 |
85587c96bbdd29905ee084f3e29df320749e923a3dee5301602a78d2ad3b30d9
|
Provenance
The following attestation bundles were made for fastapi_cachex-0.2.0.tar.gz:
Publisher:
release.yml on allen0099/FastAPI-CacheX
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_cachex-0.2.0.tar.gz -
Subject digest:
73b593a82f46f35fc9a63f340d8a914c581e27576da9ff42545c7afdd296b617 - Sigstore transparency entry: 767099236
- Sigstore integration time:
-
Permalink:
allen0099/FastAPI-CacheX@d0e72686d688545681d4891228d9baa0fd6f3a1e -
Branch / Tag:
refs/heads/master - Owner: https://github.com/allen0099
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d0e72686d688545681d4891228d9baa0fd6f3a1e -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file fastapi_cachex-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_cachex-0.2.0-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4861f99f8f9518b796f9ca5a64910ed1a1f95a91b8334a9f339d553e690030d6
|
|
| MD5 |
3c22bd5d81bf790c89b5110041cc0982
|
|
| BLAKE2b-256 |
9059d9cd76a32ebd6657d35d5549072c0a037ba8087c5e3a1e5d5d5e4d4a57f9
|
Provenance
The following attestation bundles were made for fastapi_cachex-0.2.0-py3-none-any.whl:
Publisher:
release.yml on allen0099/FastAPI-CacheX
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_cachex-0.2.0-py3-none-any.whl -
Subject digest:
4861f99f8f9518b796f9ca5a64910ed1a1f95a91b8334a9f339d553e690030d6 - Sigstore transparency entry: 767099239
- Sigstore integration time:
-
Permalink:
allen0099/FastAPI-CacheX@d0e72686d688545681d4891228d9baa0fd6f3a1e -
Branch / Tag:
refs/heads/master - Owner: https://github.com/allen0099
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d0e72686d688545681d4891228d9baa0fd6f3a1e -
Trigger Event:
workflow_dispatch
-
Statement type: