Skip to main content

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! 🚀

Metadata

Release files for cache-middleware 0.1.6

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

Source distribution (sdist)

Source distribution for cache-middleware 0.1.6
File Size Uploaded
cache_middleware-0.1.6.tar.gz 8.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for cache-middleware 0.1.6
File Interpreter ABI Platform
cache_middleware-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 MB

Release files / cache_middleware-0.1.6.tar.gz

Download URL cache_middleware-0.1.6.tar.gz
Size 8.3 MB
Tags Source
SHA-256 checksum
How to use checksums
086fe340619eb9076c0c5404cc3d1ed06a9ca579573cab447d2298634d8a9d95
BLAKE2b-256 checksum
How to use checksums
7a4550ef8a62ab5957f4d8bd4ad10f95b736c43b6f3063a71c7fcc9c31743878
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release files / cache_middleware-0.1.6-py3-none-any.whl

Download URL cache_middleware-0.1.6-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d46e63f1b400be5b35cf927b271b730c1a087664babe3f1453d3197649ade7d
BLAKE2b-256 checksum
How to use checksums
797df28fd67fda9b813672fe4dc962d15b5762cf8ba00d7dfb20009a66417fc6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

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