HTTPX Cache Persistent Offline Replay
Add a persistent caching layer to httpx with httpx-cache, replaying stale responses offline and refreshing in the background.
Code
import asyncio
import httpx
from httpx_cache import CacheControlTransport
client = httpx.AsyncClient(
transport=CacheControlTransport(
cache=httpx_cache.FileCache(cache_dir='.http_cache'),
cacheable_methods=('GET',),
cacheable_status_codes=(200, 301, 404),
)
)
async def fetch_product(product_id: int) -> dict | None:
try:
resp = await client.get(f'https://api.example.com/products/{product_id}')
resp.raise_for_status()
return resp.json()
except httpx.HTTPStatusError:
return None
async def warm_cache(ids: list[int]) -> int:
results = await asyncio.gather(
*(fetch_product(pid) for pid in ids),
return_exceptions=True,
)
return sum(1 for r in results if not isinstance(r, Exception))
async def main() -> None:
await warm_cache(list(range(10)))
# 断网场景:文件缓存仍可回放此前成功的响应
product = await fetch_product(3)
print('served from cache offline:', product is not None)
await client.aclose()
asyncio.run(main())Report a problem with this exampleOpens a pre-filled issue on GitHub.