Hishel
Hishel is an elegant HTTP caching library for Python, implementing RFC 9111 specifications to provide seamless caching integration for popular HTTP clients like HTTPX and Requests. It offers flexible storage backends, including SQLite, and supports both synchronous and asynchronous workflows with a focus on high performance and type safety. The library is actively maintained with frequent minor releases, ensuring ongoing compatibility and feature enhancements.
Warnings
- breaking Hishel 1.1.9 officially dropped support for Python 3.9. Users on Python 3.9 or older must use an earlier version of Hishel.
- gotcha The `hishel.install_cache` function is intended for experimental use only and is not recommended for production environments.
- gotcha For stream-based responses to be properly stored in Hishel storages, you must consume (iterate through) the response stream. Simply creating an entry with a stream does not store its data.
- gotcha Hishel's `CacheClient` and `AsyncCacheClient` classes hide the constructor signature of the underlying `httpx` client. This means IDEs might not suggest arguments that are still valid for configuration.
- gotcha Earlier versions (before 1.1.8) had potential race conditions in storage operations.
- gotcha Python's mutable default arguments (e.g., lists, dicts) are evaluated only once when the function is defined. Using them as defaults for `FilterPolicy` or other configurable components can lead to unintended shared state across calls.
Install
-
pip install hishel -
pip install hishel[httpx] -
pip install hishel[requests] -
pip install hishel[fastapi]
Imports
- CacheClient
from hishel import CacheClient
- AsyncCacheClient
from hishel import AsyncCacheClient
- SyncCacheClient
from hishel.httpx import SyncCacheClient
- AsyncCacheClient
from hishel.httpx import AsyncCacheClient
- CacheTransport
from hishel import CacheTransport
- SyncSqliteStorage
from hishel import SyncSqliteStorage
- AsyncSqliteStorage
from hishel import AsyncSqliteStorage
- BaseFilter
from hishel import BaseFilter
Quickstart
import hishel
import httpx
def main():
# Use SyncCacheClient for synchronous HTTPX requests
with hishel.httpx.SyncCacheClient() as client:
print("First request (from origin):")
response1 = client.get("https://httpbin.org/get")
print(f"Status: {response1.status_code}, From cache: {response1.extensions.get('hishel_from_cache', False)}")
print("\nSecond request (from cache if cachable):")
response2 = client.get("https://httpbin.org/get")
print(f"Status: {response2.status_code}, From cache: {response2.extensions.get('hishel_from_cache', False)}")
# Example with explicit SQLite storage
# Ensure 'my_cache.db' is created in a writable directory
storage = hishel.SyncSqliteStorage(database_path="./my_cache.db")
with hishel.httpx.SyncCacheClient(storage=storage) as client:
print("\nFirst request with SQLite storage (from origin):")
response3 = client.get("https://httpbin.org/get?item=1")
print(f"Status: {response3.status_code}, From cache: {response3.extensions.get('hishel_from_cache', False)}")
print("\nSecond request with SQLite storage (from cache):")
response4 = client.get("https://httpbin.org/get?item=1")
print(f"Status: {response4.status_code}, From cache: {response4.extensions.get('hishel_from_cache', False)}")
if __name__ == "__main__":
main()