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.
โจ 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
- ๐ Documentation: https://impalah.github.io/cache-middleware/
- ๐ฆ PyPI: https://pypi.org/project/cache-middleware/
- ๐ GitHub: https://github.com/impalah/cache-middleware
- ๐ Issues: https://github.com/impalah/cache-middleware/issues
๐ 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
086fe340619eb9076c0c5404cc3d1ed06a9ca579573cab447d2298634d8a9d95
|
|
| MD5 |
69d9cfafb4b52303633e5bf123d56d1b
|
|
| BLAKE2b-256 |
7a4550ef8a62ab5957f4d8bd4ad10f95b736c43b6f3063a71c7fcc9c31743878
|
File details
Details for the file cache_middleware-0.1.6-py3-none-any.whl.
File metadata
- Download URL: cache_middleware-0.1.6-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d46e63f1b400be5b35cf927b271b730c1a087664babe3f1453d3197649ade7d
|
|
| MD5 |
ef9d4701141e98b7efdfd748e28885cb
|
|
| BLAKE2b-256 |
797df28fd67fda9b813672fe4dc962d15b5762cf8ba00d7dfb20009a66417fc6
|