Skip to main content

A caching library for FastAPI with support for Cache-Control, ETag, and multiple backends.

Project description

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, providing comprehensive HTTP caching support.

Features

  • Support for HTTP caching headers
    • Cache-Control
    • ETag
    • If-None-Match
  • Multiple backend cache support
    • Redis
    • Memcached
    • In-memory cache
  • Complete Cache-Control directive implementation
  • Easy-to-use @cache decorator

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.

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.

from fastapi_cachex.backends import MemoryBackend
from fastapi_cachex import BackendProxy

backend = MemoryBackend()
BackendProxy.set_backend(backend)

Memcached

from fastapi_cachex.backends import MemcachedBackend
from fastapi_cachex import BackendProxy

backend = MemcachedBackend(servers=["localhost:11211"])
BackendProxy.set_backend(backend)

Redis

from fastapi_cachex.backends import AsyncRedisCacheBackend
from fastapi_cachex import BackendProxy

backend = AsyncRedisCacheBackend(host="127.0.1", port=6379, db=0)
BackendProxy.set_backend(backend)

Documentation

License

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

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_cachex-0.1.6.tar.gz (20.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_cachex-0.1.6-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_cachex-0.1.6.tar.gz.

File metadata

  • Download URL: fastapi_cachex-0.1.6.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastapi_cachex-0.1.6.tar.gz
Algorithm Hash digest
SHA256 35a6a91fbb62588aca7e8951a865fab4b079a18971a23266a938192f2c58ace0
MD5 445732626f60da55d90943cbde6366bc
BLAKE2b-256 5477427e262e7a715413df34569459581f24f9d0960409561779ed6d17b36eb2

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_cachex-0.1.6.tar.gz:

Publisher: publish.yml on allen0099/FastAPI-CacheX

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_cachex-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: fastapi_cachex-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastapi_cachex-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 44d30583098c7ec1f9002419949a7da30b08661354bb368b8a5d2214d6368ad2
MD5 46aec401bd9107b056258fe273f1bb07
BLAKE2b-256 1c893a54cb5cc7825595994de40e11d80bd19c4bb68020974dab9d8c24130cd6

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_cachex-0.1.6-py3-none-any.whl:

Publisher: publish.yml on allen0099/FastAPI-CacheX

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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