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, DragonflyDB and Redis.
Quickstart¶
First, install with pip (python 3.8+ 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!
Contents¶
Project Info¶
- Related Projects
- Contributing Guide
- Contributors
- History
- 0.12.0 (2024-10-01)
- 0.11.1 (2024-08-01)
- 0.11.0 (2024-02-08)
- Deprecations and Removals
- 0.10.0 (2023-10-30)
- 0.9.1 (2023-09-20)
- 0.9.0 (2023-09-19)
- 0.8.2 (2023-07-14)
- 0.8.1 (2023-01-05)
- 0.8.0 (2022-12-29)
- 0.7.3 (2022-07-31)
- 0.7.2 (2022-07-13)
- 0.7.1 (2022-06-22)
- 0.7.0 (2022-05-21)
- 0.6.1 (2022-02-13)
- 0.6.0 (2022-02-12)
- 0.5.2 (2021-11-03)
- 0.5.1 (2021-09-10)
- 0.5.0 (2021-09-01)
- 0.4.3 (2021-07-27)
- 0.4.2 (2021-07-26)
- 0.4.1 (2021-07-09)
- 0.4.0 (2021-05-12)
- 0.3.0 (2021-04-09)
- 0.2.0 (2021-02-28)
- 0.1.0 (2020-11-14)