Elegant HTTP Caching for Python
Project description
Hishel
Elegant HTTP Caching for Python
Hishel (հիշել, to remember in Armenian) is a modern HTTP caching library for Python that implements RFC 9111 specifications. It provides seamless caching integration for popular HTTP clients with minimal code changes.
✨ Features
- 🎯 RFC 9111 Compliant - Fully compliant with the latest HTTP caching specification
- 🔌 Easy Integration - Drop-in support for HTTPX, Requests, ASGI, FastAPI, and BlackSheep
- 💾 Flexible Storage - SQLite backend with more coming soon
- ⚡ High Performance - Efficient caching with minimal overhead
- 🔄 Async & Sync - Full support for both synchronous and asynchronous workflows
- 🎨 Type Safe - Fully typed with comprehensive type hints
- 🧪 Well Tested - Extensive test coverage and battle-tested
- 🎛️ Configurable - Fine-grained control over caching behavior with flexible policies
- 💨 Memory Efficient - Streaming support prevents loading large payloads into memory
- 🌐 Universal - Works with any ASGI application (Starlette, Litestar, BlackSheep, etc.)
- 🎯 GraphQL Support - Cache GraphQL queries with body-sensitive content caching
📦 Installation
pip install hishel
Optional Dependencies
Install with specific integration support:
pip install hishel[httpx] # For HTTPX support
pip install hishel[requests] # For Requests support
pip install hishel[fastapi] # For FastAPI support (includes ASGI)
Or install multiple:
pip install hishel[httpx,requests,fastapi]
[!NOTE] ASGI middleware has no extra dependencies - it's included in the base installation.
🚀 Quick Start
With HTTPX
Synchronous:
from hishel.httpx import SyncCacheClient
client = SyncCacheClient()
# First request - fetches from origin
response = client.get("https://api.example.com/data")
print(response.extensions["hishel_from_cache"]) # False
# Second request - served from cache
response = client.get("https://api.example.com/data")
print(response.extensions["hishel_from_cache"]) # True
Asynchronous:
from hishel.httpx import AsyncCacheClient
async with AsyncCacheClient() as client:
# First request - fetches from origin
response = await client.get("https://api.example.com/data")
print(response.extensions["hishel_from_cache"]) # False
# Second request - served from cache
response = await client.get("https://api.example.com/data")
print(response.extensions["hishel_from_cache"]) # True
With Requests
import requests
from hishel.requests import CacheAdapter
session = requests.Session()
session.mount("https://", CacheAdapter())
session.mount("http://", CacheAdapter())
# First request - fetches from origin
response = session.get("https://api.example.com/data")
# Second request - served from cache
response = session.get("https://api.example.com/data")
print(response.headers.get("X-Hishel-From-Cache")) # "True"
With ASGI Applications
Add caching middleware to any ASGI application:
from hishel.asgi import ASGICacheMiddleware
# Wrap your ASGI app
app = ASGICacheMiddleware(app)
# Or configure with options
from hishel import AsyncSqliteStorage, CacheOptions, SpecificationPolicy
app = ASGICacheMiddleware(
app,
storage=AsyncSqliteStorage(),
policy=SpecificationPolicy(
cache_options=CacheOptions(shared=True)
),
)
With FastAPI
Add Cache-Control headers using the cache() dependency:
from fastapi import FastAPI
from hishel.fastapi import cache
app = FastAPI()
@app.get("/api/data", dependencies=[cache(max_age=300, public=True)])
async def get_data():
# Cache-Control: public, max-age=300
return {"data": "cached for 5 minutes"}
# Optionally wrap with ASGI middleware for local caching according to specified rules
from hishel.asgi import ASGICacheMiddleware
from hishel import AsyncSqliteStorage
app = ASGICacheMiddleware(app, storage=AsyncSqliteStorage())
With BlackSheep
Use BlackSheep's native cache_control decorator with Hishel's ASGI middleware:
from blacksheep import Application, get
from blacksheep.server.headers.cache import cache_control
app = Application()
@get("/api/data")
@cache_control(max_age=300, public=True)
async def get_data():
# Cache-Control: public, max-age=300
return {"data": "cached for 5 minutes"}
🎛️ Advanced Configuration
Caching Policies
Hishel supports two types of caching policies:
SpecificationPolicy - RFC 9111 compliant HTTP caching (default):
from hishel import CacheOptions, SpecificationPolicy
from hishel.httpx import SyncCacheClient
client = SyncCacheClient(
policy=SpecificationPolicy(
cache_options=CacheOptions(
shared=False, # Use as private cache (browser-like)
supported_methods=["GET", "HEAD", "POST"], # Cache GET, HEAD, and POST
allow_stale=True # Allow serving stale responses
)
)
)
FilterPolicy - Custom filtering logic for fine-grained control:
from hishel import FilterPolicy, BaseFilter, Request
from hishel.httpx import AsyncCacheClient
class CacheOnlyAPIRequests(BaseFilter[Request]):
def needs_body(self) -> bool:
return False
def apply(self, item: Request, body: bytes | None) -> bool:
return "/api/" in str(item.url)
client = AsyncCacheClient(
policy=FilterPolicy(
request_filters=[CacheOnlyAPIRequests()]
)
)
Custom Storage Backend
from hishel import SyncSqliteStorage
from hishel.httpx import SyncCacheClient
storage = SyncSqliteStorage(
database_path="my_cache.db",
default_ttl=7200.0, # Cache entries expire after 2 hours
refresh_ttl_on_access=True # Reset TTL when accessing cached entries
)
client = SyncCacheClient(storage=storage)
GraphQL and Body-Sensitive Caching
Cache GraphQL queries and other POST requests by including the request body in the cache key.
Using per-request header:
from hishel import FilterPolicy
from hishel.httpx import SyncCacheClient
client = SyncCacheClient(
policy=FilterPolicy()
)
# Cache GraphQL queries - different queries get different cache entries
graphql_query = """
query GetUser($id: ID!) {
user(id: $id) {
name
email
}
}
"""
response = client.post(
"https://api.example.com/graphql",
json={"query": graphql_query, "variables": {"id": "123"}},
headers={"X-Hishel-Body-Key": "true"} # Enable body-based caching
)
# Different query will be cached separately
response = client.post(
"https://api.example.com/graphql",
json={"query": graphql_query, "variables": {"id": "456"}},
headers={"X-Hishel-Body-Key": "true"}
)
Using global configuration:
from hishel.httpx import SyncCacheClient
from hishel import FilterPolicy
# Enable body-based caching for all requests
client = SyncCacheClient(policy=FilterPolicy(use_body_key=True))
# All POST requests automatically include body in cache key
response = client.post(
"https://api.example.com/graphql",
json={"query": graphql_query, "variables": {"id": "123"}}
)
🏗️ Architecture
Hishel uses a sans-I/O state machine architecture that separates HTTP caching logic from I/O operations:
- ✅ Correct - Fully RFC 9111 compliant
- ✅ Testable - Easy to test without network dependencies
- ✅ Flexible - Works with any HTTP client or server
- ✅ Type Safe - Clear state transitions with full type hints
🔮 Roadmap
We're actively working on:
- 🎯 Performance optimizations
- 🎯 More integrations
- 🎯 Partial responses support
📚 Documentation
Comprehensive documentation is available at https://hishel.com/dev
- Getting Started
- HTTPX Integration
- Requests Integration
- ASGI Integration
- FastAPI Integration
- BlackSheep Integration
- GraphQL Integration
- Storage Backends
- Request/Response Metadata
- RFC 9111 Specification
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
See our Contributing Guide for more details.
📄 License
This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.
💖 Support
If you find Hishel useful, please consider:
- ⭐ Starring the repository
- 🐛 Reporting bugs and issues
- 💡 Suggesting new features
- 📖 Improving documentation
- ☕ Buying me a coffee
🙏 Acknowledgments
Hishel is inspired by and builds upon the excellent work in the Python HTTP ecosystem, particularly:
- HTTPX - A next-generation HTTP client for Python
- Requests - The classic HTTP library for Python
- RFC 9111 - HTTP Caching specification
Made with ❤️ by Kar Petrosyan
Changelog
All notable changes to this project will be documented in this file.
1.0.0b1 - 2025-10-28
♻️ Refactoring
- Add policies
⚙️ Miscellaneous Tasks
- Improve sans-io diagram colors
- Add graphql docs
🐛 Bug Fixes
- Body-sensitive responses caching
- Filter out
Transfer-Encodingheader for asgi responses
🚀 Features
- Add global
use_body_keysetting
1.0.0.dev3 - 2025-10-26
♻️ Refactoring
- Replace pairs with entries, simplify storage API
- Automatically generate httpx sync integration from async
⚙️ Miscellaneous Tasks
- Simplify metadata docs
- Add custom integrations docs
- More robust compressed response caching
🐛 Bug Fixes
- Add missing permissions into
publish.yml - Raise on consumed httpx streams, which we can't store as is (it's already decoded)
- Fix compressed data caching for requests
- Handle httpx iterable usage instead of iterator correctly
- Add date header for proper age calculation
🚀 Features
- Add integrations with fastapi and asgi
- Add blacksheep integration examples
- Add logging for asgi
1.0.0.dev2 - 2025-10-21
⚙️ Miscellaneous Tasks
- Remove redundant utils and tests
- Add import without extras check in ci
- Fix time travel date, explicitly specify the timezone
🐛 Bug Fixes
- Fix check for storing auth requests
- Don't raise an error on 3xx during revalidation
🚀 Features
- Add hishel_created_at response metadata
1.0.0.dev1 - 2025-10-21
⚙️ Miscellaneous Tasks
- Remove some redundant utils methods
📦 Dependencies
- Make httpx and async libs optional dependencies
- Make
anysqliteoptional dependency - Install async extra with httpx
- Improve git-cliff
1.0.0.dev0 - 2025-10-19
⚙️ Miscellaneous Tasks
- Use mike powered versioning
- Improve docs versioning, deploy dev doc on ci
0.1.5 - 2025-10-18
⚙️ Miscellaneous Tasks
- Remove some redundant files from repo
🐛 Bug Fixes
- Fix some line breaks
🚀 Features
- Set chunk size to 128KB for httpx to reduce SQLite read/writes
- Better cache-control parsing
- Add close method to storages API (#384)
- Increase requests buffer size to 128KB, disable charset detection
0.1.4 - 2025-10-14
⚙️ Miscellaneous Tasks
- Improve CI (#369)
- Remove src folder (#373)
- Temporary remove python3.14 from CI
- Add sqlite tests for new storage
- Move some tests to beta
🐛 Bug Fixes
- Create an sqlite file in a cache folder
- Fix beta imports
🚀 Features
- Add support for a sans-IO API (#366)
- Allow already consumed streams with
CacheTransport(#377) - Add sqlite storage for beta storages
- Get rid of some locks from sqlite storage
- Better async implemetation for sqlite storage
Project details
Release history Release notifications | RSS feed
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 hishel-1.0.0b1.tar.gz.
File metadata
- Download URL: hishel-1.0.0b1.tar.gz
- Upload date:
- Size: 58.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f4e0f92414b27bd92ed7b2d10efc8cc619f07b503d6a6106562b2d0e53c1353
|
|
| MD5 |
5719457405158d6919f3cb669097d1ba
|
|
| BLAKE2b-256 |
f96c78107d3c4881f01dc763b5b165e410b48d296f01a59ddd4c6ca2522d7341
|
Provenance
The following attestation bundles were made for hishel-1.0.0b1.tar.gz:
Publisher:
publish.yml on karpetrosyan/hishel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hishel-1.0.0b1.tar.gz -
Subject digest:
8f4e0f92414b27bd92ed7b2d10efc8cc619f07b503d6a6106562b2d0e53c1353 - Sigstore transparency entry: 649150540
- Sigstore integration time:
-
Permalink:
karpetrosyan/hishel@f6b8e854d1de2807f9cac309c807a6a51578a329 -
Branch / Tag:
refs/tags/1.0.0b1 - Owner: https://github.com/karpetrosyan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f6b8e854d1de2807f9cac309c807a6a51578a329 -
Trigger Event:
push
-
Statement type:
File details
Details for the file hishel-1.0.0b1-py3-none-any.whl.
File metadata
- Download URL: hishel-1.0.0b1-py3-none-any.whl
- Upload date:
- Size: 68.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b88017cbe2ccedade207c07e1f2c37e8bcd4fd424b3de38fc3579a5ee174d72
|
|
| MD5 |
9ae48a05ea9ec34d7b5c64d0e1778cb9
|
|
| BLAKE2b-256 |
899bdb02de88e554fb2649fe1db40151d580524ec3fe70535200c1ef30ab5381
|
Provenance
The following attestation bundles were made for hishel-1.0.0b1-py3-none-any.whl:
Publisher:
publish.yml on karpetrosyan/hishel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hishel-1.0.0b1-py3-none-any.whl -
Subject digest:
8b88017cbe2ccedade207c07e1f2c37e8bcd4fd424b3de38fc3579a5ee174d72 - Sigstore transparency entry: 649150578
- Sigstore integration time:
-
Permalink:
karpetrosyan/hishel@f6b8e854d1de2807f9cac309c807a6a51578a329 -
Branch / Tag:
refs/tags/1.0.0b1 - Owner: https://github.com/karpetrosyan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f6b8e854d1de2807f9cac309c807a6a51578a329 -
Trigger Event:
push
-
Statement type: