Skip to main content

Persistent cache for aiohttp requests

Project description

aiohttp-client-cache

Build status Documentation Status Coverage Status PyPI PyPI - Python Versions PyPI - Format

See full documentation at https://aiohttp-client-cache.readthedocs.io

aiohttp-client-cache is an async persistent cache for aiohttp requests, based on requests-cache.

Not to be confused with aiohttp-cache, which is a cache for the aiohttp web server. This package is, as you might guess, specifically for the aiohttp client.

Development Status

This is an early work in progress!

The current state is a working drop-in replacement (or mixin) for aiohttp.ClientSession, with multiple asynchronous cache backends.

Breaking changes should be expected until a 1.0 release.

Installation

Requires python 3.7+

Install the latest stable version with pip:

pip install aiohttp-client-cache

Note: You will need additional dependencies depending on which backend you want to use; See Cache Backends section below for details. To install with extra dependencies for all supported backends:

pip install aiohttp-client-cache[backends]

See Contributing for setup info for local development.

Usage Examples

See the examples folder for more detailed usage examples.

Here is a simple example using an endpoint that takes 1 second to fetch. After the first request, subsequent requests to the same URL will return near-instantly; so, fetching it 10 times will only take ~1 second instead of 10.

from aiohttp_client_cache import CachedSession, SQLiteBackend

async with CachedSession(cache=SQLiteBackend()) as session:
    for i in range(10):
        await session.get('http://httpbin.org/delay/1')

Here is an example with more customized behavior:

cache = SQLiteBackend(
    cache_name='~/.cache/aiohttp-requests.db',     # For SQLite, this will be used as the filename
    expire_after=24,                               # By default, cached responses expire in a day
    expire_after_urls={'*.site.com/static': 24*7}, # Requests with this pattern will expire in a week
    ignored_params=['auth_token'],                 # Ignore this param when caching responses
)

async with CachedSession(cache=cache) as session:
    await session.get('https://site.com/index.html')             # Expires in a day
    await session.get('https://img.site.com/static/a27bf6.jpg')  # Expires in a week

See CacheBackend for more usage details.

aiohttp-client-cache can also be used as a mixin, if you happen have other mixin classes that you want to combine with it:

from aiohttp import ClientSession
from aiohttp_client_cache import CacheMixin

class CustomSession(CacheMixin, CustomMixin, ClientSession):
    pass

Cache Backends

Several backends are available. If one isn't specified, a non-persistent in-memory cache will be used.

Incomplete:

You can also provide your own backend by subclassing aiohttp_client_cache.backends.BaseCache.

Expiration

If you are using the expire_after parameter, expired responses are removed from the storage the next time the same request is made. If you want to manually purge all expired items, you can use CachedSession.delete_expired_responses. Example:

session = CachedSession(expire_after=3)   # Cached responses expire after 3 hours
await session.remove_expired_responses()  # Remove any responses over 3 hours old

Conditional Caching

Caching behavior can be customized by defining various conditions:

  • Response status codes
  • Request HTTP methods
  • Request headers
  • Specific request parameters
  • Custom filter function

See CacheBackend docs for details.

Credits

Thanks to Roman Haritonov and contributors for the original requests-cache!

This project is licensed under the MIT license, with the exception of storage backend code adapted from requests-cache, which is licensed under the BSD license (copy included).

History

0.2.0 (2021-02-28)

  • Refactor SQLite backend to use aiosqlite for async cache operations
  • Refactor MongoDB backend to use motor for async cache operations
  • Refactor Redis backend to use aiosqlite for async cache operations
  • Add integration tests and docker-compose for local test servers

0.1.0 (2020-11-14)

  • Initial PyPI release
  • First pass at a general refactor and conversion from requests to aiohttp
  • Basic features are functional, but some backends do not actually operate asynchronously

requests-cache

See requests-cache development history for details on prior changes.

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

aiohttp-client-cache-0.2.1.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

aiohttp_client_cache-0.2.1-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-client-cache-0.2.1.tar.gz.

File metadata

  • Download URL: aiohttp-client-cache-0.2.1.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.5

File hashes

Hashes for aiohttp-client-cache-0.2.1.tar.gz
Algorithm Hash digest
SHA256 819915f3b2cd50200af38b3d0631a8c9724a3e880702ee483ecf8cb449caf21e
MD5 e1ab8de6aeff1ad50ccd620f5c4e733c
BLAKE2b-256 87f71fb87ee5c203b95079bd04dabdcbb3b534a1ac34fd92fc0d8616d5399223

See more details on using hashes here.

File details

Details for the file aiohttp_client_cache-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_client_cache-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.5

File hashes

Hashes for aiohttp_client_cache-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ae152340be99e527e4862965fef42286f34b1fb65eb85ce86e3530754bcd504e
MD5 8b09c3587b682e33ec50a166adfd718a
BLAKE2b-256 8b8d03f1cfabacb5c41e83bcd41b45e3243ca58d30090efd39a8dc9d44d918e3

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