Skip to main content

Elegant HTTP Caching for Python

Project description

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 and Requests
  • 💾 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
  • 🌐 Future Ready - Designed for easy integration with any HTTP client/server

📦 Installation

pip install hishel

Optional Dependencies

Install with specific HTTP client support:

pip install hishel[httpx]      # For HTTPX support
pip install hishel[requests]   # For Requests support

Or install both:

pip install hishel[httpx,requests]

🚀 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"

🎛️ Advanced Configuration

Custom Cache Options

from hishel import CacheOptions
from hishel.httpx import SyncCacheClient

client = SyncCacheClient(
    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
    )
)

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)

🏗️ 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

While Hishel currently supports HTTPX and Requests, we're actively working on:

  • 🎯 Additional HTTP client integrations
  • 🎯 Server-side caching support
  • 🎯 More storage backends
  • 🎯 Advanced caching strategies
  • 🎯 Performance optimizations

📚 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

Changelog

All notable changes to this project will be documented in this file.

1.0.0.dev2 - 2025-10-21

⚙️ Miscellaneous Tasks

  • Remove redundant utils and tests
  • Add import without extras check in ci

🐛 Bug Fixes

  • Fix check for storing auth requests
  • Don't raise an error on 3xx during revalidation

🚀 Features

  • Add hishel_created_at response metadata

1.0.0.dev1 - 2025-10-21

⚙️ Miscellaneous Tasks

  • Remove some redundant utils methods

📦 Dependencies

  • Make httpx and async libs optional dependencies
  • Make anysqlite optional dependency
  • Install async extra with httpx
  • Improve git-cliff

1.0.0.dev0 - 2025-10-19

⚙️ Miscellaneous Tasks

  • Use mike powered versioning
  • Improve docs versioning, deploy dev doc on ci

0.1.5 - 2025-10-18

⚙️ Miscellaneous Tasks

  • Remove some redundant files from repo

🐛 Bug Fixes

  • Fix some line breaks

🚀 Features

  • Set chunk size to 128KB for httpx to reduce SQLite read/writes
  • Better cache-control parsing
  • Add close method to storages API (#384)
  • Increase requests buffer size to 128KB, disable charset detection

0.1.4 - 2025-10-14

⚙️ Miscellaneous Tasks

  • Improve CI (#369)
  • Remove src folder (#373)
  • Temporary remove python3.14 from CI
  • Add sqlite tests for new storage
  • Move some tests to beta

🐛 Bug Fixes

  • Create an sqlite file in a cache folder
  • Fix beta imports

🚀 Features

  • Add support for a sans-IO API (#366)
  • Allow already consumed streams with CacheTransport (#377)
  • Add sqlite storage for beta storages
  • Get rid of some locks from sqlite storage
  • Better async implemetation for sqlite storage

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hishel-1.0.0.dev2.tar.gz (52.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hishel-1.0.0.dev2-py3-none-any.whl (56.8 kB view details)

Uploaded Python 3

File details

Details for the file hishel-1.0.0.dev2.tar.gz.

File metadata

  • Download URL: hishel-1.0.0.dev2.tar.gz
  • Upload date:
  • Size: 52.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hishel-1.0.0.dev2.tar.gz
Algorithm Hash digest
SHA256 2e932d60e50f5ca9898992c82718b5e45cc17bebb125725da7322f16fcbb936f
MD5 0e663ff7f1da8df086181f216edfb4c9
BLAKE2b-256 c7a087c7a13a0af6d0819a7bf29554c607d9fd21e827ec9cb63d407a12f0430e

See more details on using hashes here.

Provenance

The following attestation bundles were made for hishel-1.0.0.dev2.tar.gz:

Publisher: publish.yml on karpetrosyan/hishel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hishel-1.0.0.dev2-py3-none-any.whl.

File metadata

  • Download URL: hishel-1.0.0.dev2-py3-none-any.whl
  • Upload date:
  • Size: 56.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hishel-1.0.0.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 af6b6c776479f1fc5e3899dc7fbd408af974b54f13ac0ace13137e9010399a28
MD5 3de28a32db890897cae6ce43bd564ed5
BLAKE2b-256 bb7f83b955fbcc10db75793c7a21f953d64dac3695f4c3a81d97f7bc15dba80f

See more details on using hashes here.

Provenance

The following attestation bundles were made for hishel-1.0.0.dev2-py3-none-any.whl:

Publisher: publish.yml on karpetrosyan/hishel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page