ASGI Integration
Hishel provides ASGI middleware for caching HTTP responses in any ASGI-compatible application (FastAPI, Starlette, Django ASGI, etc.).
The middleware intercepts requests and responses, caching them according to HTTP caching specifications (RFC 9111) or custom rules.
Installation
pip install hishel
No extra dependencies required - ASGI support is included by default.
Quick Start
Wrap your ASGI application with ASGICacheMiddleware
:
from hishel.asgi import ASGICacheMiddleware
# Your ASGI application
async def app(scope, receive, send):
if scope["type"] == "http":
await send({
"type": "http.response.start",
"status": 200,
"headers": [[b"cache-control", b"max-age=3600"]],
})
await send({
"type": "http.response.body",
"body": b"Hello, World!",
})
# Wrap with caching middleware
cached_app = ASGICacheMiddleware(app)
Basic Usage
HTTP Caching with Hishel
Hishel provides elegant HTTP caching middleware for ASGI applications.
Installation
pip install hishel fastapi aiohttp litestar blacksheep uvicorn
Examples
# fastapi_example.py
from fastapi import FastAPI
from hishel import ASGICacheMiddleware
app = FastAPI()
@app.get("/")
async def index():
return {"message": "Hello, world!"}
app = ASGICacheMiddleware(app)
# Run: uvicorn fastapi_example:app --reload
# litestar_example.py
from litestar import Litestar, get
from hishel import ASGICacheMiddleware
@get("/")
async def index() -> str:
return "Hello, world!"
app = ASGICacheMiddleware(Litestar([index]))
# Run: uvicorn litestar_example:app --reload
# blacksheep_example.py
from blacksheep import Application, get
from hishel import ASGICacheMiddleware
app = Application()
@get("/")
async def index():
return "Hello, world!"
app = ASGICacheMiddleware(app)
# Run: uvicorn blacksheep_example:app --reload
See Also
- Storage Backends - Configure cache storage
- Request/Response Metadata - Control caching behavior
- FastAPI Integration - FastAPI-specific helpers