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.

Development Status

This library is a work in progress!

Breaking changes should be expected until a 1.0 release, so version pinning is recommended.

My goal for this library is to eventually have a similar (but not identical) feature set as requests-cache, and also contribute new features from this library back to requests-cache. 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!

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:

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

Uploaded Source

Built Distribution

aiohttp_client_cache-0.7.3-py3-none-any.whl (32.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiohttp-client-cache-0.7.3.tar.gz
  • Upload date:
  • Size: 56.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0a2 CPython/3.9.13 Linux/5.4.0-1086-azure

File hashes

Hashes for aiohttp-client-cache-0.7.3.tar.gz
Algorithm Hash digest
SHA256 c9757c91db9eb14765cc976574c69a8587b5597959542263eb83217dadc220a9
MD5 09f7e85eedc38d9f682c0d44e8569c16
BLAKE2b-256 6dd8382f3eb0e7bac077ed7ee757f6f08fc8da2a8f6250143fe6244e74f566a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp_client_cache-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c7532e3763b2dc92fb32de2ab0a1dcdbda608219b1564418fd2c4cff14082929
MD5 c50c8a94b0e44f52e5c74e1df6b71f37
BLAKE2b-256 cd2adecb138242b4458b44f5625c81219a687778fab7b93e420987364f2d9f6d

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