# 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.
[](https://pypi.org/project/cache-middleware/)
[](https://pypi.org/project/cache-middleware/)
[](https://github.com/impalah/cache-middleware/blob/main/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):**
```bash
pip install cache-middleware
```
**With Redis backend:**
```bash
pip install cache-middleware[redis]
```
**With Memcached backend:**
```bash
pip install cache-middleware[memcached]
```
**With all backends:**
```bash
pip install cache-middleware[all]
```
### Basic Usage
```python
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
```python
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](LICENSE) file for details.
## 🤝 Contributing
Contributions are welcome! Please read our [Contributing Guide](https://impalah.github.io/cache-middleware/) 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:
```bash
# 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
```bash
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`](scripts/README.md).
## 🚀 Getting Started
Ready to add caching to your FastAPI application? Check out our [User Guide](https://impalah.github.io/cache-middleware/user-guide.html) for detailed examples and best practices.
For advanced configuration and custom backends, see our [Configuration Guide](https://impalah.github.io/cache-middleware/middleware-configuration.html).
---
**Cache Middleware** - Making FastAPI applications faster, one cache at a time! 🚀
Raw data
{
"_id": null,
"home_page": null,
"name": "cache-middleware",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": null,
"keywords": "cache, fastapi, middleware, starlette",
"author": null,
"author_email": "Linus <impalah@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/7a/45/50ef8a62ab5957f4d8bd4ad10f95b736c43b6f3063a71c7fcc9c31743878/cache_middleware-0.1.6.tar.gz",
"platform": null,
"description": "# Cache Middleware\n\nA high-performance HTTP response caching solution for FastAPI and Starlette applications.\n\nCache Middleware provides transparent response caching with pluggable backends, following the Starlette middleware pattern for seamless integration.\n\n[](https://pypi.org/project/cache-middleware/)\n[](https://pypi.org/project/cache-middleware/)\n[](https://github.com/impalah/cache-middleware/blob/main/LICENSE)\n\n## \u2728 Features\n\n- **\ud83d\udd04 Multiple Backends**: Redis, Memcached, in-memory, and custom backend support\n- **\ud83c\udfaf Decorator-based**: Simple `@cache(timeout=300)` decorator for endpoint caching\n- **\ud83d\udccb Cache-Control Support**: Respects HTTP Cache-Control headers\n- **\u2699\ufe0f Flexible Configuration**: Environment-based or explicit configuration\n- **\ud83d\ude80 Production Ready**: Comprehensive error handling and logging\n- **\ud83d\udd12 Type Safe**: Full type hints and mypy support\n- **\ud83d\udce6 Modular Installation**: Install only the backends you need\n\n## \ud83d\udcd6 Documentation\n\nComplete documentation is available at: **https://impalah.github.io/cache-middleware/**\n\n## \ud83d\ude80 Quick Start\n\n### Installation\n\n**Basic installation (memory backend only):**\n\n```bash\npip install cache-middleware\n```\n\n**With Redis backend:**\n\n```bash\npip install cache-middleware[redis]\n```\n\n**With Memcached backend:**\n\n```bash\npip install cache-middleware[memcached]\n```\n\n**With all backends:**\n\n```bash\npip install cache-middleware[all]\n```\n\n### Basic Usage\n\n```python\nfrom fastapi import FastAPI\nfrom cache_middleware import CacheMiddleware, MemoryBackend, cache\n\napp = FastAPI()\n\n# Configure memory backend\nmemory_backend = MemoryBackend(max_size=1000)\napp.add_middleware(CacheMiddleware, backend=memory_backend)\n\n@app.get(\"/items\")\n@cache(timeout=300) # Cache for 5 minutes\nasync def get_items():\n return {\"items\": [1, 2, 3, 4, 5]}\n```\n\n### Redis/ValKey Backend Example\n\n```python\nfrom cache_middleware import RedisBackend\n\n# Configure Redis backend\nredis_backend = RedisBackend(url=\"redis://localhost:6379\")\napp.add_middleware(CacheMiddleware, backend=redis_backend)\n\n# Or configure ValKey backend (same API)\nvalkey_backend = RedisBackend(url=\"redis://localhost:6380\")\napp.add_middleware(CacheMiddleware, backend=valkey_backend)\n```\n\n## \ud83d\udd27 Backend Options\n\n### Memory Backend\n\n- \u2705 No external dependencies\n- \u2705 Perfect for development and testing\n- \u2705 LRU eviction policy\n- \u2705 Configurable size limits\n\n### Redis/ValKey Backend\n\n- \u2705 Production-ready distributed caching\n- \u2705 Persistence and high availability\n- \u2705 Clustering support\n- \u2705 High performance with hiredis\n- \u2705 **Redis & ValKey Compatible**: Works with both Redis and ValKey servers\n\n### Memcached Backend\n\n- \u2705 Lightweight distributed caching\n- \u2705 Simple and fast\n- \u2705 Wide ecosystem support\n\n### Custom Backends\n\n- \u2705 Implement your own backend\n- \u2705 Simple abstract interface\n- \u2705 Easy integration\n\n## \ud83d\udccb Requirements\n\n- **Python**: 3.12 or higher\n- **FastAPI**: 0.116.1 or higher\n- **Redis/ValKey**: Optional, required only for Redis/ValKey backend\n- **Memcached**: Optional, required only for Memcached backend\n\n## \ud83d\udd17 Links\n\n- **\ud83d\udcd6 Documentation**: https://impalah.github.io/cache-middleware/\n- **\ud83d\udce6 PyPI**: https://pypi.org/project/cache-middleware/\n- **\ud83d\udc19 GitHub**: https://github.com/impalah/cache-middleware\n- **\ud83d\udc1b Issues**: https://github.com/impalah/cache-middleware/issues\n\n## \ud83d\udcc4 License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## \ud83e\udd1d Contributing\n\nContributions are welcome! Please read our [Contributing Guide](https://impalah.github.io/cache-middleware/) for details on our code of conduct and the process for submitting pull requests.\n\n## \ud83d\udee0\ufe0f Development\n\n### Version Management\n\nThis project uses automatic version synchronization across all files. When you bump the version:\n\n```bash\n# Development commands\nmake bump-version # Bumps patch version (0.1.5 \u2192 0.1.6)\nmake bump-version PART=minor # Bumps minor version (0.1.5 \u2192 0.2.0)\nmake bump-version PART=major # Bumps major version (0.1.5 \u2192 1.0.0)\n\n# Sync only (without bumping)\nmake sync-version # Synchronizes current version across all files\n```\n\nThe version is automatically updated in:\n\n- `pyproject.toml` (source of truth)\n- `src/cache_middleware/__init__.py` (`__version__`)\n- `docs_source/conf.py` (Sphinx documentation)\n\n### Build and Test\n\n```bash\nmake build # Build package (includes version bump)\nmake test # Run tests\nmake lint # Code linting\nmake format # Code formatting\nmake docs # Build documentation\n```\n\nFor more details, see [`scripts/README.md`](scripts/README.md).\n\n## \ud83d\ude80 Getting Started\n\nReady to add caching to your FastAPI application? Check out our [User Guide](https://impalah.github.io/cache-middleware/user-guide.html) for detailed examples and best practices.\n\nFor advanced configuration and custom backends, see our [Configuration Guide](https://impalah.github.io/cache-middleware/middleware-configuration.html).\n\n---\n\n**Cache Middleware** - Making FastAPI applications faster, one cache at a time! \ud83d\ude80\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Cache capabilities middleware for Starlette/Fastapi",
"version": "0.1.6",
"project_urls": null,
"split_keywords": [
"cache",
" fastapi",
" middleware",
" starlette"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "797df28fd67fda9b813672fe4dc962d15b5762cf8ba00d7dfb20009a66417fc6",
"md5": "ef9d4701141e98b7efdfd748e28885cb",
"sha256": "7d46e63f1b400be5b35cf927b271b730c1a087664babe3f1453d3197649ade7d"
},
"downloads": -1,
"filename": "cache_middleware-0.1.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ef9d4701141e98b7efdfd748e28885cb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 17006,
"upload_time": "2025-08-05T07:30:42",
"upload_time_iso_8601": "2025-08-05T07:30:42.790677Z",
"url": "https://files.pythonhosted.org/packages/79/7d/f28fd67fda9b813672fe4dc962d15b5762cf8ba00d7dfb20009a66417fc6/cache_middleware-0.1.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "7a4550ef8a62ab5957f4d8bd4ad10f95b736c43b6f3063a71c7fcc9c31743878",
"md5": "69d9cfafb4b52303633e5bf123d56d1b",
"sha256": "086fe340619eb9076c0c5404cc3d1ed06a9ca579573cab447d2298634d8a9d95"
},
"downloads": -1,
"filename": "cache_middleware-0.1.6.tar.gz",
"has_sig": false,
"md5_digest": "69d9cfafb4b52303633e5bf123d56d1b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 8269772,
"upload_time": "2025-08-05T07:30:44",
"upload_time_iso_8601": "2025-08-05T07:30:44.822660Z",
"url": "https://files.pythonhosted.org/packages/7a/45/50ef8a62ab5957f4d8bd4ad10f95b736c43b6f3063a71c7fcc9c31743878/cache_middleware-0.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-05 07:30:44",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "cache-middleware"
}