Hishel

1.1.9 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates using Hishel's `SyncCacheClient` with HTTPX for both in-memory (default) and file-based (SQLite) caching. The first request fetches data from the origin, while subsequent requests for the same URL will be served from the cache, if applicable according to HTTP caching rules.

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()

view raw JSON →