aiohttp-client-cache

0.14.3 · active · verified Tue Apr 14

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.

Warnings

Install

Imports

Quickstart

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.

import asyncio
from aiohttp_client_cache import CachedSession, SQLiteBackend

async def main():
    # Initialize CachedSession with a SQLite backend
    # 'demo_cache' will be the filename for the SQLite database
    async with CachedSession(cache=SQLiteBackend('demo_cache')) as session:
        print("First request (will be slow...")
        response = await session.get('http://httpbin.org/delay/1')
        print(f"Response from cache: {response.from_cache}, Status: {response.status}")

        print("Second request (should be fast...")
        response = await session.get('http://httpbin.org/delay/1')
        print(f"Response from cache: {response.from_cache}, Status: {response.status}")

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →