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

Quickstart

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

pip install aiohttp-client-cache

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!

Metadata

Release files for aiohttp-client-cache 0.8.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohttp-client-cache 0.8.2
File Size Uploaded
aiohttp_client_cache-0.8.2.tar.gz 56.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-client-cache 0.8.2
File Interpreter ABI Platform
aiohttp_client_cache-0.8.2-py3-none-any.whl Python 3 none any Details

Total release size: 87.8 kB

Release files / aiohttp_client_cache-0.8.2.tar.gz

Download URL aiohttp_client_cache-0.8.2.tar.gz
Size 56.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fe090ae89477f6702e3e3a622ca65404125696e51dbdb91edaa12740f2f1f274
BLAKE2b-256 checksum
How to use checksums
0619f1b5337a12f705b1ba2cc22132ca9c0d1208397de7f8fec91a55414bfcd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Linux/5.15.0-1041-azure

Release files / aiohttp_client_cache-0.8.2-py3-none-any.whl

Download URL aiohttp_client_cache-0.8.2-py3-none-any.whl
Size 31.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e47fb53606aa1f84944fae37264fc40fef6feef9e7281b8afd93807968c6920a
BLAKE2b-256 checksum
How to use checksums
fabcb1e31d43d37c01b6941b1c42236dcdf102104aee0bf221befa2dd9f61d1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Linux/5.15.0-1041-azure

Release history Release notifications | RSS feed

0.14.2

2 release files

0.14.0

2 release files

0.12.4

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

This release

0.8.2 This release

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release 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