Persistent cache implementation for httpx and httpcore following RFC 9111 specification
74
Drop-in replacements for httpx.Client and httpx.AsyncClient that provide transparent HTTP caching. These clients automatically cache responses according to HTTP caching rules and serve cached responses for subsequent requests.
A synchronous HTTP client with built-in caching capabilities, fully compatible with httpx.Client.
class CacheClient(httpx.Client):
def __init__(self, *, storage=None, controller=None, **kwargs):
"""
Initialize a synchronous HTTP client with caching.
Parameters:
- storage: Storage backend for cached responses (defaults to FileStorage)
- controller: Cache controller for caching logic (defaults to Controller())
- **kwargs: All httpx.Client parameters (timeout, headers, auth, etc.)
"""Usage Examples:
import hishel
# Basic usage with default file storage
with hishel.CacheClient() as client:
response = client.get("https://api.example.com/data")
# Custom storage backend
storage = hishel.RedisStorage()
with hishel.CacheClient(storage=storage) as client:
response = client.get("https://api.example.com/data")
# Custom cache controller
controller = hishel.Controller(
cacheable_methods=["GET", "POST"],
cacheable_status_codes=[200, 204, 301]
)
with hishel.CacheClient(controller=controller) as client:
response = client.get("https://api.example.com/data")
# All httpx.Client parameters work
with hishel.CacheClient(
timeout=30.0,
headers={'User-Agent': 'MyApp/1.0'},
auth=('username', 'password')
) as client:
response = client.get("https://api.example.com/data")An asynchronous HTTP client with built-in caching capabilities, fully compatible with httpx.AsyncClient.
class AsyncCacheClient(httpx.AsyncClient):
def __init__(self, *, storage=None, controller=None, **kwargs):
"""
Initialize an asynchronous HTTP client with caching.
Parameters:
- storage: Async storage backend for cached responses (defaults to AsyncFileStorage)
- controller: Cache controller for caching logic (defaults to Controller())
- **kwargs: All httpx.AsyncClient parameters (timeout, headers, auth, etc.)
"""Usage Examples:
import hishel
import asyncio
async def main():
# Basic usage with default async file storage
async with hishel.AsyncCacheClient() as client:
response = await client.get("https://api.example.com/data")
# Custom async storage backend
storage = hishel.AsyncRedisStorage()
async with hishel.AsyncCacheClient(storage=storage) as client:
response = await client.get("https://api.example.com/data")
# Custom cache controller with async client
controller = hishel.Controller(allow_heuristics=True)
async with hishel.AsyncCacheClient(controller=controller) as client:
response = await client.get("https://api.example.com/data")
asyncio.run(main())Both clients support per-request cache control through request extensions:
# Force cache usage for a specific request
response = client.get("https://api.example.com/data",
extensions={"force_cache": True})
# Disable cache for a specific request
response = client.get("https://api.example.com/data",
extensions={"force_cache": False})Cache clients fully support all HTTPX features:
Install with Tessl CLI
npx tessl i tessl/pypi-hisheldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10