Persistent cache for aiohttp requests
Project description
aiohttp-client-cache
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:
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for aiohttp-client-cache-0.4.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7b763e4fdae7d00ef93331aaee806cf2491e85785d0b14731d7debc8dec86fc |
|
MD5 | 63c8c49e26f16327a037d4a56bf757bd |
|
BLAKE2b-256 | 3a5986138f9cdd60a5d1124266548bc5dbbfe00f4205253f9923b4df01592c20 |
Hashes for aiohttp_client_cache-0.4.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fe2dd7054774b490cfb9ae1e238a13d5f7b16b6a04d6d3614df2484b6ccde8b |
|
MD5 | edb1918ec7c9045b48665a690ebed0e1 |
|
BLAKE2b-256 | 933ad894e3fa3a69c82f74b195d91ebd1e58cbfffe7a661fdb1086e306a3ad76 |