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.
Common errors
-
ModuleNotFoundError: No module named 'hishel._serializers'
cause This error typically occurs when there's a version mismatch between `hishel` and a dependent library, or when upgrading `hishel` to a major version (like 1.0.0) that changed internal module structure, while the application or another dependency expects an older structure.fixEnsure `hishel` and any libraries using it are compatible. If using `hishel==1.0.0` or newer, and a dependent library expects an older version, try pinning `hishel` to the version specified by the dependent library (e.g., `pip install hishel==0.1.3`). If possible, update the dependent library to a version compatible with the latest `hishel`. -
ModuleNotFoundError: No module named 'hishel'
cause This error means the `hishel` package is not installed in the Python environment being used, or the environment is not correctly configured for the Python interpreter.fixInstall the `hishel` library using pip: `pip install hishel`. If using specific integrations, install with extras, e.g., `pip install hishel[httpx]` or `pip install hishel[requests]`. -
ImportError: cannot import name 'SyncCacheClient' from 'hishel'
cause The caching client classes for specific HTTP libraries (like HTTPX or Requests) are located in submodules, not directly under the top-level `hishel` package.fixImport `SyncCacheClient` (or `AsyncCacheClient`, `CacheClient`, `CacheAdapter`) from their respective integration submodules. For HTTPX, use `from hishel.httpx import SyncCacheClient`. For Requests, use `from hishel.requests import CacheAdapter`. -
ModuleNotFoundError: No module named 'httpx'
cause This error occurs when trying to use `hishel`'s HTTPX integration (e.g., `SyncCacheClient` or `AsyncCacheClient`) without having the `httpx` library installed as a dependency.fixInstall `httpx` explicitly or install `hishel` with the `httpx` extra: `pip install httpx` or `pip install hishel[httpx]`.
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()