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.middleware import CacheMiddleware
from cache_middleware.backends.memory_backend import MemoryBackend
from cache_middleware.decorators import 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 Backend Example

from cache_middleware.backends.redis_backend import RedisBackend

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

🔧 Backend Options

Memory Backend

  • ✅ No external dependencies
  • ✅ Perfect for development and testing
  • ✅ LRU eviction policy
  • ✅ Configurable size limits

Redis Backend

  • ✅ Production-ready distributed caching
  • ✅ Persistence and high availability
  • ✅ Clustering support
  • ✅ High performance with hiredis

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: Optional, required only for Redis 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.

🚀 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.5.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.5-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cache_middleware-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 f3ad3a6a7d44a314285c0cb444ee4caaff64e7f295b9a94ae08a5fa286646aae
MD5 62a59169bd56d70359a2518c027cf848
BLAKE2b-256 79f4aa5df3c69c27434067b9e9656fdf96e1775502a82f4a77cd5db8d011dadf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cache_middleware-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 89ed42fbdb2a10469706972a96a15fb7086f761a86d7db7f0f6d2efac36b3253
MD5 1a1430cabaef30cf928f1e71af5161ac
BLAKE2b-256 cccbee169582ad59bd60ce770ce764031e530f5046a0f944edd4193af05936c9

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