Skip to main content

Persistent cache for aiohttp requests

Project description

aiohttp-client-cache

Build status Documentation Status Codecov PyPI Conda PyPI - Python Versions PyPI - Format

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

Features

  • Ease of use: Use as a drop-in replacement for aiohttp.ClientSession
  • Customization: Works out of the box with little to no config, but with plenty of options available for customizing cache expiration and other behavior
  • Persistence: Includes several storage backends: SQLite, DynamoDB, MongoDB, and Redis.

Quickstart

First, install with pip (python 3.7+ required):

pip install aiohttp-client-cache[all]

Note: Adding [all] will install optional dependencies for all supported backends. When adding this library to your application, you can include only the dependencies you actually need; see individual backend docs and pyproject.toml for details.

Basic Usage

Next, use aiohttp_client_cache.CachedSession in place of aiohttp.ClientSession. To briefly demonstrate how to use it:

Replace this:

from aiohttp import ClientSession

async with ClientSession() as session:
    await session.get('http://httpbin.org/delay/1')

With this:

from aiohttp_client_cache import CachedSession, SQLiteBackend

async with CachedSession(cache=SQLiteBackend('demo_cache')) as session:
    await session.get('http://httpbin.org/delay/1')

The URL in this example adds a delay of 1 second, simulating a slow or rate-limited website. With caching, the response will be fetched once, saved to demo_cache.sqlite, and subsequent requests will return the cached response near-instantly.

Configuration

Several options are available to customize caching behavior. This example demonstrates a few of them:

# fmt: off
from aiohttp_client_cache import SQLiteBackend

cache = SQLiteBackend(
    cache_name='~/.cache/aiohttp-requests.db',  # For SQLite, this will be used as the filename
    expire_after=60*60,                         # By default, cached responses expire in an hour
    urls_expire_after={'*.fillmurray.com': -1}, # Requests for any subdomain on this site will never expire
    allowed_codes=(200, 418),                   # Cache responses with these status codes
    allowed_methods=['GET', 'POST'],            # Cache requests with these HTTP methods
    include_headers=True,                       # Cache requests with different headers separately
    ignored_params=['auth_token'],              # Keep using the cached response even if this param changes
    timeout=2.5,                                # Connection timeout for SQLite backend
)

More Info

To learn more, see:

Feedback

If there is a feature you want, if you've discovered a bug, or if you have other general feedback, please create an issue for it!

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

Uploaded Source

Built Distribution

aiohttp_client_cache-0.9.0-py3-none-any.whl (30.2 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_client_cache-0.9.0.tar.gz.

File metadata

  • Download URL: aiohttp_client_cache-0.9.0.tar.gz
  • Upload date:
  • Size: 55.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1011-azure

File hashes

Hashes for aiohttp_client_cache-0.9.0.tar.gz
Algorithm Hash digest
SHA256 d51ff802c5e52d74dec6cd2f0ec9d9a6ad3e5f477560badd1640068e15afef51
MD5 69fed6f91ba43a190c5f894e56e1a5a8
BLAKE2b-256 cf6e470261feb1115823559e9112bd9b7e4a59b1ea62a6a10c48a710ad19963e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp_client_cache-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef043726f4cec221d1792177cd3a727899a21db37be887bf661468df5313d8d3
MD5 c2a41e035b1f8d109dc8cbd49e83f5e7
BLAKE2b-256 f2d47672f0fc05042ab9a028a151b8b4abe290825b8a44253f425b648692f5f3

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