Skip to main content

Cache capabilities middleware for Starlette/Fastapi

Project description

Cache Middleware

A high-performance HTTP response caching solution for FastAPI and Starlette applications.

Cache Middleware provides transparent response caching with pluggable backends, following the Starlette middleware pattern for seamless integration.

PyPI Version Python Version License

โœจ Features

  • ๐Ÿ”„ Multiple Backends: Redis, Memcached, in-memory, and custom backend support
  • ๐ŸŽฏ Decorator-based: Simple @cache(timeout=300) decorator for endpoint caching
  • ๐Ÿ“‹ Cache-Control Support: Respects HTTP Cache-Control headers
  • โš™๏ธ Flexible Configuration: Environment-based or explicit configuration
  • ๐Ÿš€ Production Ready: Comprehensive error handling and logging
  • ๐Ÿ”’ Type Safe: Full type hints and mypy support
  • ๐Ÿ“ฆ Modular Installation: Install only the backends you need

๐Ÿ“– Documentation

Complete documentation is available at: https://impalah.github.io/cache-middleware/

๐Ÿš€ Quick Start

Installation

Basic installation (memory backend only):

pip install cache-middleware

With Redis backend:

pip install cache-middleware[redis]

With Memcached backend:

pip install cache-middleware[memcached]

With all backends:

pip install cache-middleware[all]

Basic Usage

from fastapi import FastAPI
from cache_middleware import CacheMiddleware, MemoryBackend, cache

app = FastAPI()

# Configure memory backend
memory_backend = MemoryBackend(max_size=1000)
app.add_middleware(CacheMiddleware, backend=memory_backend)

@app.get("/items")
@cache(timeout=300)  # Cache for 5 minutes
async def get_items():
    return {"items": [1, 2, 3, 4, 5]}

Redis/ValKey Backend Example

from cache_middleware import RedisBackend

# Configure Redis backend
redis_backend = RedisBackend(url="redis://localhost:6379")
app.add_middleware(CacheMiddleware, backend=redis_backend)

# Or configure ValKey backend (same API)
valkey_backend = RedisBackend(url="redis://localhost:6380")
app.add_middleware(CacheMiddleware, backend=valkey_backend)

๐Ÿ”ง Backend Options

Memory Backend

  • โœ… No external dependencies
  • โœ… Perfect for development and testing
  • โœ… LRU eviction policy
  • โœ… Configurable size limits

Redis/ValKey Backend

  • โœ… Production-ready distributed caching
  • โœ… Persistence and high availability
  • โœ… Clustering support
  • โœ… High performance with hiredis
  • โœ… Redis & ValKey Compatible: Works with both Redis and ValKey servers

Memcached Backend

  • โœ… Lightweight distributed caching
  • โœ… Simple and fast
  • โœ… Wide ecosystem support

Custom Backends

  • โœ… Implement your own backend
  • โœ… Simple abstract interface
  • โœ… Easy integration

๐Ÿ“‹ Requirements

  • Python: 3.12 or higher
  • FastAPI: 0.116.1 or higher
  • Redis/ValKey: Optional, required only for Redis/ValKey backend
  • Memcached: Optional, required only for Memcached backend

๐Ÿ”— Links

๐Ÿ“„ License

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

๐Ÿค Contributing

Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.

๐Ÿ› ๏ธ Development

Version Management

This project uses automatic version synchronization across all files. When you bump the version:

# Development commands
make bump-version           # Bumps patch version (0.1.5 โ†’ 0.1.6)
make bump-version PART=minor # Bumps minor version (0.1.5 โ†’ 0.2.0)
make bump-version PART=major # Bumps major version (0.1.5 โ†’ 1.0.0)

# Sync only (without bumping)
make sync-version           # Synchronizes current version across all files

The version is automatically updated in:

  • pyproject.toml (source of truth)
  • src/cache_middleware/__init__.py (__version__)
  • docs_source/conf.py (Sphinx documentation)

Build and Test

make build     # Build package (includes version bump)
make test      # Run tests
make lint      # Code linting
make format    # Code formatting
make docs      # Build documentation

For more details, see scripts/README.md.

๐Ÿš€ Getting Started

Ready to add caching to your FastAPI application? Check out our User Guide for detailed examples and best practices.

For advanced configuration and custom backends, see our Configuration Guide.


Cache Middleware - Making FastAPI applications faster, one cache at a time! ๐Ÿš€

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

cache_middleware-0.1.6.tar.gz (8.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cache_middleware-0.1.6-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cache_middleware-0.1.6.tar.gz
  • Upload date:
  • Size: 8.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for cache_middleware-0.1.6.tar.gz
Algorithm Hash digest
SHA256 086fe340619eb9076c0c5404cc3d1ed06a9ca579573cab447d2298634d8a9d95
MD5 69d9cfafb4b52303633e5bf123d56d1b
BLAKE2b-256 7a4550ef8a62ab5957f4d8bd4ad10f95b736c43b6f3063a71c7fcc9c31743878

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cache_middleware-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7d46e63f1b400be5b35cf927b271b730c1a087664babe3f1453d3197649ade7d
MD5 ef9d4701141e98b7efdfd748e28885cb
BLAKE2b-256 797df28fd67fda9b813672fe4dc962d15b5762cf8ba00d7dfb20009a66417fc6

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