Skip to main content

Simple HTTP cache for Python Requests

Project description

HTTP, like all well designed standards, has multiple confusing mechanisms for caching. httpcache is a HTTP cache that knows how to use HTTP headers and status codes to correctly cache your HTTP traffic. It’s built for use with the excellent Requests library, because if you’re not using Requests you’re probably prepared to roll your own caching library too.

It’s gloriously easy to use. If all you want is caching in Requests, all you need to do is plug a transport adapter into your Requests session:

>>> import requests
>>> from httpcache import CachingHTTPAdapter
>>> s = requests.Session()
>>> s.mount('http://', CachingHTTPAdapter())

Away you go!

If you want more control, you can use the cache data-store itself. Store your cache entries like this:

from httpcache import HTTPCache
cache = HTTPCache(capacity=50)
cache.store(response)

And retrieve them like this:

cached_response = cache.retrieve(request)

Simple.

Features

  • Tight integration with Requests data structures.

  • Understands Expires and Cache-Control headers.

  • Knows how to interpret 304 Not Modified responses.

  • Can send If-Modified-Since headers.

  • Aware of HTTP verbs, e.g. POST.

  • RFC 2616-compliant.

Installation

To install httpcache, you want to run:

$ pip install httpcache

If you can’t do that, and you really must have httpcache, and you can’t install pip, then you can try:

$ easy_install httpcache

I strongly recommend you don’t do that though.

Versions

httpcache supports all the versions of Python that Requests does. This means 2.6, 2.7 and 3.3. It is possible that httpcache will work on other versions of Python but we do not test on those versions and will not support them.

Contribute

Contributions are always welcome! Please abide by the following rules when contributing:

  1. Check that no-one has opened an issue already covering your bug. If you open a duplicate issue, the maintainer will give you a stern look.

  2. Fork the Github repository and start writing your tests. If you’re fixing a bug, I recommend writing a failing test first and working until it passes. If you’re adding a feature, you’re free to add tests after you write the functionality, but please test the functionality thoroughly.

  3. Send a Pull Request. If I don’t respond within a couple of days, please shout at me on Twitter or via email until I do something about it.

History

0.1.3 (2013-05-19)

  • Support Python 2.6

  • Support Python 3.3

0.1.2 (2013-05-18)

  • Fix broken setup.py.

0.1.1 (2013-05-18)

  • Cache a broader set of response codes.

  • Follow RFC 2616’s recommendations about caching resources with query strings.

  • Don’t cache non-idempotent methods.

  • Non-idempotent methods invalidate their cache resources.

  • Documentation!

  • Actually implement the capacity parameter.

0.1.0 (2013-05-11)

  • Provide a Requests Transport Adapter so that caching ‘Just Works’.

0.0.2 (2013-05-07)

  • Correctly cache using the ‘Expires’ header.

  • Correctly handle some of the functionality exposed by the ‘Cache-Control’ header.

0.0.1 (2013-05-05)

  • Conception

  • If-Modified-Since and 304 handling.

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

httpcache-0.1.3.tar.gz (12.8 kB view details)

Uploaded Source

File details

Details for the file httpcache-0.1.3.tar.gz.

File metadata

  • Download URL: httpcache-0.1.3.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for httpcache-0.1.3.tar.gz
Algorithm Hash digest
SHA256 32c2b811129e4f54d6ec4beb2b260e047f1ec7bf9dfb6494cbc2cbc43bb174a5
MD5 80db07a6914dbdcdad69561a19c33bb2
BLAKE2b-256 e2f02223cedd18e2bc7e73453d32e84079507a795e51ea80c3fc9da78a1563c8

See more details on using hashes here.

Supported by

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