Skip to main content

Persistant cache implementation for httpx and httpcore

Project description

hishel

PyPI - Version PyPI - Python Version


Hishel (հիշել, remember) is a library that implements HTTP Caching for HTTPX and HTTP Core libraries in accordance with RFC 9111, the most recent caching specification.

Features

  • 💾 Persistence: Responses are cached in the persistent memory for later use.
  • 🤲 Compatibility: It is very simple to integrate with your existing httpx client, transport, or httpcore pool.
  • 🤗 Easy to use: You continue to use the httpx and httpcore interfaces. Can be integrated with no changes to the code.
  • 🧠 Smart: Attempts to clearly implement RFC 9111, understands Vary, Etag, Last-Modified, Cache-Control, and Expires headers, and handles response re-validation automatically.

QuickStart

Install Hishel using pip:

$ pip install hishel

Let's begin with an example of a httpx client.

import hishel

client = hishel.CacheClient()
client.get("https://www.github.com")
client.get("https://www.github.com")  # takes from the cache (very fast!)

If the response is cacheable according to RFC 9111, hishel will save it for later use, so the user only needs to change the client and the rest of the staff will be done automatically.

By default, Hishel stores responses in the ./cache/hishel directory, but this behavior can be overridden by explicitly specifying storage.

If we look, we can see that a new file was created in that directory. This is our serialized response. Hishel uses the json serializer by default, but we can explicitly set the other supported serializers, such as YAML and Pickle serializers.

This is how the file looks.

{
    "status": 301,
    "headers": [
        [
            "Server",
            "nginx"
        ],
        [
            "Date",
            "Fri, 21 Jul 2023 14:23:50 GMT"
        ],
        [
            "Content-Length",
            "0"
        ],
        [
            "Connection",
            "keep-alive"
        ],
        [
            "Location",
            "https://httpbun.org"
        ],
        [
            "X-Powered-By",
            "httpbun/3c0dc05883dd9212ac38b04705037d50b02f2596"
        ]
    ],
    "content": "",
    "extensions": {
        "http_version": "HTTP/1.1",
        "reason_phrase": "Moved Permanently"
    }
}

There is all the information required to rebuild the response, including the content value encoded in base64.

Hishel also works well with httpcore, and you can make your httpcore connection pools cacheable with a single line of code.

Your existing code

from httpcore import ConnectionPool

pool = ConnectionPool()

...

Adding HTTP caching to your program will make it much faster and more efficient.

import hishel
from httpcore import ConnectionPool

pool = ConnectionPool()
pool = hishel.CacheConnectionPool(pool=pool)
...

As you can see, it is extremely simple to integrate.

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-0.0.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

hishel-0.0.1-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file hishel-0.0.1.tar.gz.

File metadata

  • Download URL: hishel-0.0.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.1

File hashes

Hashes for hishel-0.0.1.tar.gz
Algorithm Hash digest
SHA256 36e1009f6e0e1e2688dc2b2b3c6ff120fe26a48af5abf0c257d122cd3cc7a529
MD5 aea6711fda4eb728822de2ca0c27e5dd
BLAKE2b-256 329e85de4e1eac4ab5abaaa333bc361b1d6b83db299792256616256c13f11faf

See more details on using hashes here.

File details

Details for the file hishel-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: hishel-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.1

File hashes

Hashes for hishel-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8904cca58da0165adc08cb74e1d0b503f2783470001e3d567ed21541ab6ae561
MD5 e589cbe12cf7e8dbc14df3096c6f6360
BLAKE2b-256 e2e8de797bd0283b7fb9ff13784d49b11e724b815afd8767d64ae25beaa7e7a9

See more details on using hashes here.

Supported by

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