Skip to main content

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, DragonflyDB and Redis.

Quickstart

First, install with pip (python 3.9+ 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!

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

Uploaded Source

Built Distribution

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

aiohttp_client_cache-0.14.2-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiohttp_client_cache-0.14.2.tar.gz
  • Upload date:
  • Size: 63.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiohttp_client_cache-0.14.2.tar.gz
Algorithm Hash digest
SHA256 d3f4b81204de9dce59894d9ecb9c77a8dfd633d73c920bbcf8a7370650656585
MD5 f659518c5ca4cc9ef241d2f2111c520a
BLAKE2b-256 29afb8e2f6ce10f0f71d675386a785966532a3e72db6d67558bab533f9cdb94a

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_client_cache-0.14.2.tar.gz:

Publisher: deploy.yml on requests-cache/aiohttp-client-cache

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

File details

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

File metadata

File hashes

Hashes for aiohttp_client_cache-0.14.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5286d6c46fa822d0dec12d5110648de675050dcc4226e2f1e4fd162ea5f24fda
MD5 3da6479d2c22eb35c12447a421d1c53c
BLAKE2b-256 ae6121e37e403942fb607cb93391ff2d7084318554332393e2cc5a9ba2ff7e7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_client_cache-0.14.2-py3-none-any.whl:

Publisher: deploy.yml on requests-cache/aiohttp-client-cache

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

Release history Release notifications | RSS feed

0.14.3

2 files

This release

0.14.2 This release

2 files

0.14.1

2 files

0.14.0

2 files

0.13.0

2 files

0.12.4

2 files

0.12.3

2 files

0.12.2

2 files

0.12.1

2 files

0.12.0

2 files

0.11.1

2 files

0.11.0

2 files

0.10.0

2 files

0.9.1

2 files

0.9.0

2 files

0.8.2

2 files

0.8.1

2 files

0.8.0

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.5.0b1

2 files

0.5.0b0

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page