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 and Requests
- ๐พ 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
- ๐ Future Ready - Designed for easy integration with any HTTP client/server
๐ฆ Installation
pip install hishel
Optional Dependencies
Install with specific HTTP client support:
pip install hishel[httpx] # For HTTPX support
pip install hishel[requests] # For Requests support
Or install both:
pip install hishel[httpx,requests]
๐ 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"
๐๏ธ Advanced Configuration
Custom Cache Options
from hishel import CacheOptions
from hishel.httpx import SyncCacheClient
client = SyncCacheClient(
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
)
)
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)
๐๏ธ 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
While Hishel currently supports HTTPX and Requests, we're actively working on:
- ๐ฏ Additional HTTP client integrations
- ๐ฏ Server-side caching support
- ๐ฏ More storage backends
- ๐ฏ Advanced caching strategies
- ๐ฏ Performance optimizations
๐ Documentation
Comprehensive documentation is available at https://hishel.com/dev
๐ค 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