Skip to content

Hishel Logo Hishel Logo

Hishel

Elegant HTTP Caching for Python

PyPI version Python versions License Coverage Downloads


Hishel (ีฐีซีทีฅีฌ, to remember in Armenian) is a modern HTTP caching library for Python that implements RFC 9111 specifications. It provides seamless caching integration for popular HTTP clients with minimal code changes.

โœจ Features

  • ๐ŸŽฏ RFC 9111 Compliant - Fully compliant with the latest HTTP caching specification
  • ๐Ÿ”Œ Easy Integration - Drop-in support for HTTPX, Requests, ASGI, FastAPI, and BlackSheep
  • ๐Ÿ’พ Flexible Storage - SQLite backend with more coming soon
  • โšก High Performance - Efficient caching with minimal overhead
  • ๐Ÿ”„ Async & Sync - Full support for both synchronous and asynchronous workflows
  • ๐ŸŽจ Type Safe - Fully typed with comprehensive type hints
  • ๐Ÿงช Well Tested - Extensive test coverage and battle-tested
  • ๐ŸŽ›๏ธ Configurable - Fine-grained control over caching behavior with flexible policies
  • ๐Ÿ’จ Memory Efficient - Streaming support prevents loading large payloads into memory
  • ๐ŸŒ Universal - Works with any ASGI application (Starlette, Litestar, BlackSheep, etc.)
  • ๐ŸŽฏ GraphQL Support - Cache GraphQL queries with body-sensitive content caching

๐Ÿ“ฆ Installation

pip install hishel

Optional Dependencies

Install with specific integration support:

pip install hishel[httpx]      # For HTTPX support
pip install hishel[requests]   # For Requests support
pip install hishel[fastapi]    # For FastAPI support (includes ASGI)

Or install multiple:

pip install hishel[httpx,requests,fastapi]

Note

ASGI middleware has no extra dependencies - it's included in the base installation.

๐Ÿš€ Quick Start

With HTTPX

Synchronous:

from hishel.httpx import SyncCacheClient

client = SyncCacheClient()

# First request - fetches from origin
response = client.get("https://api.example.com/data")
print(response.extensions["hishel_from_cache"])  # False

# Second request - served from cache
response = client.get("https://api.example.com/data")
print(response.extensions["hishel_from_cache"])  # True

Asynchronous:

from hishel.httpx import AsyncCacheClient

async with AsyncCacheClient() as client:
    # First request - fetches from origin
    response = await client.get("https://api.example.com/data")
    print(response.extensions["hishel_from_cache"])  # False

    # Second request - served from cache
    response = await client.get("https://api.example.com/data")
    print(response.extensions["hishel_from_cache"])  # True

With Requests

import requests
from hishel.requests import CacheAdapter

session = requests.Session()
session.mount("https://", CacheAdapter())
session.mount("http://", CacheAdapter())

# First request - fetches from origin
response = session.get("https://api.example.com/data")

# Second request - served from cache
response = session.get("https://api.example.com/data")
print(response.headers.get("X-Hishel-From-Cache"))  # "True"

With ASGI Applications

Add caching middleware to any ASGI application:

from hishel.asgi import ASGICacheMiddleware

# Wrap your ASGI app
app = ASGICacheMiddleware(app)

# Or configure with options
from hishel import AsyncSqliteStorage, CacheOptions, SpecificationPolicy

app = ASGICacheMiddleware(
    app,
    storage=AsyncSqliteStorage(),
    policy=SpecificationPolicy(
      cache_options=CacheOptions(shared=True)
    )
)

With FastAPI

Add Cache-Control headers using the cache() dependency:

from fastapi import FastAPI
from hishel.fastapi import cache

app = FastAPI()

@app.get("/api/data", dependencies=[cache(max_age=300, public=True)])
async def get_data():
    # Cache-Control: public, max-age=300
    return {"data": "cached for 5 minutes"}

# Optionally wrap with ASGI middleware for local caching according to specified rules
from hishel.asgi import ASGICacheMiddleware
from hishel import AsyncSqliteStorage

app = ASGICacheMiddleware(app, storage=AsyncSqliteStorage())

With BlackSheep

Use BlackSheep's native cache_control decorator with Hishel's ASGI middleware:

from blacksheep import Application, get
from blacksheep.server.headers.cache import cache_control

app = Application()

@get("/api/data")
@cache_control(max_age=300, public=True)
async def get_data():
    # Cache-Control: public, max-age=300
    return {"data": "cached for 5 minutes"}

๐ŸŽ›๏ธ Advanced Configuration

Caching Policies

Hishel supports two types of caching policies for flexible caching strategies:

SpecificationPolicy (RFC 9111 Compliant)

The default policy that follows HTTP caching standards:

from hishel import CacheOptions, SpecificationPolicy
from hishel.httpx import SyncCacheClient

client = SyncCacheClient(
    policy=SpecificationPolicy(
      cache_options=CacheOptions(
          shared=False,                              # Use as private cache (browser-like)
          supported_methods=["GET", "HEAD", "POST"], # Cache GET, HEAD, and POST
          allow_stale=True                           # Allow serving stale responses
      )
    )
)

FilterPolicy (Custom Filtering)

Apply custom logic to determine what gets cached:

from hishel import FilterPolicy, BaseFilter, Request
from hishel.httpx import AsyncCacheClient

class CacheOnlyAPIRequests(BaseFilter[Request]):
    def needs_body(self) -> bool:
        return False

    def apply(self, item: Request, body: bytes | None) -> bool:
        # Only cache requests to /api/ endpoints
        return "/api/" in str(item.url)

client = AsyncCacheClient(
    policy=FilterPolicy(
        request_filters=[CacheOnlyAPIRequests()]
    )
)

Learn More

See the Policies Guide for detailed examples including GraphQL caching, body inspection, and combining multiple filters.

Custom Storage Backend

from hishel import SyncSqliteStorage
from hishel.httpx import SyncCacheClient

storage = SyncSqliteStorage(
    database_path="my_cache.db",
    default_ttl=7200.0,           # Cache entries expire after 2 hours
    refresh_ttl_on_access=True    # Reset TTL when accessing cached entries
)

client = SyncCacheClient(storage=storage)

GraphQL and Body-Sensitive Caching

Cache GraphQL queries and other POST requests by including the request body in the cache key.

Using per-request header:

from hishel.httpx import SyncCacheClient

client = SyncCacheClient()

# Cache GraphQL queries - different queries get different cache entries
graphql_query = """
    query GetUser($id: ID!) {
        user(id: $id) {
            name
            email
        }
    }
"""

response = client.post(
    "https://api.example.com/graphql",
    json={"query": graphql_query, "variables": {"id": "123"}},
    headers={"X-Hishel-Body-Key": "true"}  # Enable body-based caching
)

# Different query will be cached separately
response = client.post(
    "https://api.example.com/graphql",
    json={"query": graphql_query, "variables": {"id": "456"}},
    headers={"X-Hishel-Body-Key": "true"}
)

Using global configuration:

from hishel.httpx import SyncCacheClient
from hishel import FilterPolicy

# Enable body-based caching for all requests
client = SyncCacheClient(policy=FilterPolicy(use_body_key=True))

# All POST requests automatically include body in cache key
response = client.post(
    "https://api.example.com/graphql",
    json={"query": graphql_query, "variables": {"id": "123"}}
)

๐Ÿ—๏ธ Architecture

Hishel uses a sans-I/O state machine architecture that separates HTTP caching logic from I/O operations:

  • โœ… Correct - Fully RFC 9111 compliant
  • โœ… Testable - Easy to test without network dependencies
  • โœ… Flexible - Works with any HTTP client or server
  • โœ… Type Safe - Clear state transitions with full type hints

๐Ÿ”ฎ Roadmap

We're actively working on:

  • ๐ŸŽฏ Performance optimizations
  • ๐ŸŽฏ More integrations
  • ๐ŸŽฏ Partial responses support

๐Ÿ“š Documentation

Comprehensive documentation is available at https://hishel.com/dev

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

See our Contributing Guide for more details.

๐Ÿ“„ License

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.

๐Ÿ’– Support

If you find Hishel useful, please consider:

  • โญ Starring the repository
  • ๐Ÿ› Reporting bugs and issues
  • ๐Ÿ’ก Suggesting new features
  • ๐Ÿ“– Improving documentation
  • โ˜• Buying me a coffee

๐Ÿ™ Acknowledgments

Hishel is inspired by and builds upon the excellent work in the Python HTTP ecosystem, particularly:

  • HTTPX - A next-generation HTTP client for Python
  • Requests - The classic HTTP library for Python
  • RFC 9111 - HTTP Caching specification

Made with โค๏ธ by Kar Petrosyan