aiohttp-client-cache is an async persistent cache for aiohttp client requests, based on requests-cache. It provides a CachedSession that acts as a drop-in replacement for `aiohttp.ClientSession`, offering various configurable storage backends and cache expiration strategies. The current version is 0.14.3, and it receives regular updates for bug fixes and compatibility.
pip install aiohttp-client-cacheVerified import paths — ran on the pinned version, not inferred.
This example demonstrates basic usage of CachedSession with a SQLite backend. The first request to 'httpbin.org/delay/1' will fetch the data and store it in 'demo_cache.sqlite'. Subsequent requests to the same URL will retrieve the response instantly from the cache. The `response.from_cache` attribute indicates if the response was served from the cache.
Clear existing cache files or databases after updating, or simply allow the cache to repopulate.
Upgrade your Python environment to 3.9 or higher.
Refer to the documentation on 'Cache Expiration' and 'Cache-Control' to understand how different expiration settings interact and ensure desired caching behavior.
Always explicitly configure a persistent backend (e.g., `SQLiteBackend`, `RedisBackend`) when you need data to persist across application runs. Example: `CachedSession(cache=SQLiteBackend('my_cache.sqlite'))`.Use `allowed_methods` and `allowed_codes` parameters in your backend configuration to cache additional methods or status codes. Example: `SQLiteBackend(allowed_methods=('GET', 'POST'), allowed_codes=(200, 404))`.Pin your dependency to a specific minor version and thoroughly test when upgrading to newer minor or patch versions.
Install the package using pip: 'pip install aiohttp-client-cache'.
Ensure that 'aiohttp' is installed correctly and check for circular imports in your code. Also, avoid naming your script 'aiohttp.py' to prevent conflicts.
Ensure that 'aiohttp-client-cache' is installed and use the correct import statement: 'from aiohttp_client_cache import CachedSession'.
Verify that 'CachedSession' is initialized correctly with a valid cache backend, for example: 'session = CachedSession(cache=SQLiteBackend('demo_cache'))'.Ensure that the required backend dependencies are installed. For Redis, install the package with the Redis extras: 'pip install aiohttp-client-cache[redis]'.