aiohttp_client_cache.backends.base module

class aiohttp_client_cache.backends.base.BaseCache[source]

Bases: object

A wrapper for lower-level cache storage operations. This is separate from CacheBackend to allow a single backend to contain multiple cache objects.

This is no longer using a dict-like interface due to lack of python syntax support for async dict operations.

_abc_impl = <_abc_data object>
abstract async clear()[source]

Delete all items from the cache

abstract async contains(key)[source]

Check if a key is stored in the cache

Return type

bool

Parameters

key (str) –

abstract async delete(key)[source]

Delete a single item from the cache. Does not raise an error if the item is missing.

Parameters

key (str) –

abstract async keys()[source]

Get all keys stored in the cache

Return type

Iterable[str]

async pop(key, default=None)[source]

Delete an item from the cache, and return the deleted item

Return type

Union[CachedResponse, None, bytes, str]

Parameters

key (str) –

abstract async read(key)[source]

Read a single item from the cache. Returns None if the item is missing.

Return type

Union[CachedResponse, None, bytes, str]

Parameters

key (str) –

abstract async size()[source]

Get the number of items in the cache

Return type

int

static unpickle(result)[source]
abstract async values()[source]

Get all values stored in the cache

Return type

Iterable[Union[CachedResponse, None, bytes, str]]

abstract async write(key, item)[source]

Write an item to the cache

Parameters
class aiohttp_client_cache.backends.base.CacheBackend(cache_name='aiohttp-cache', expire_after=None, allowed_codes=(200, ), allowed_methods=('GET', 'HEAD'), include_headers=False, ignored_params=None, filter_fn=<function CacheBackend.<lambda>>)[source]

Bases: object

Base class for cache backends. This manages higher-level cache operations, including cache expiration, generating cache keys, and managing redirect history.

If instantiated directly, CacheBackend will use a non-persistent in-memory cache.

Lower-level storage operations are handled by BaseCache. To extend this with your own custom backend, implement one or more subclasses of BaseCache to use as CacheBackend.responses and CacheBackend.response_aliases.

Parameters
  • cache_name (str) – Cache prefix or namespace, depending on backend; see notes below

  • expire_after (Union[int, timedelta, None]) – Number of hours after which a cache entry will expire; se None to never expire

  • allowed_codes (tuple) – Limit caching only for response with this codes

  • allowed_methods (tuple) – Cache only requests of this methods

  • include_headers (bool) – Make request headers part of the cache key

  • ignored_params (Optional[Iterable]) – List of request parameters to be excluded from the cache key.

  • filter_fn (Callable) – function that takes a aiohttp.ClientResponse object and returns a boolean indicating whether or not that response should be cached. Will be applied to both new and previously cached responses

The cache_name parameter will be used as follows depending on the backend:

  • sqlite: Cache filename prefix, e.g my_cache.sqlite

  • mongodb: Database name

  • redis: Namespace, meaning all keys will be prefixed with 'cache_name:'

Note on cache key parameters: Set include_get_headers=True if you want responses to be cached under different keys if they only differ by headers. You may also provide ignored_parameters to ignore specific request params. This is useful, for example, when requesting the same resource with different credentials or access tokens.

_remove_ignored_parameters(url, params, data)[source]
async clear()[source]

Clear cache

create_key(method, url, params=None, data=None, headers=None, **kwargs)[source]

Create a unique cache key based on request details

Return type

str

Parameters
  • method (str) –

  • url (Union[str, yarl.URL]) –

  • params (dict) –

  • data (dict) –

  • headers (dict) –

async delete(key)[source]

Delete a response from the cache, along with its history (if applicable)

Parameters

key (str) –

async delete_expired_responses()[source]

Deletes entries from cache with creation time older than expire_after. Note: Also deletes any cache items that are filtered out according to filter_fn() and filter parameters (allowable_*)

async delete_url(url)[source]

Delete cached response associated with url, along with its history (if applicable). Works only for GET requests.

Parameters

url (str) –

async get_response(key)[source]

Retrieve response and timestamp for key if it’s stored in cache, otherwise returns None`

Parameters

key (str) – key of resource

Return type

Optional[CachedResponse]

async has_url(url)[source]

Returns True if cache has url, False otherwise. Works only for GET request urls

Return type

bool

Parameters

url (str) –

is_cacheable(response)[source]

Perform all checks needed to determine if the given response should be cached

Return type

bool

Parameters

response (Optional[Union[aiohttp.client_reqrep.ClientResponse, aiohttp_client_cache.response.CachedResponse]]) –

async save_response(key, response)[source]

Save response to cache

Parameters
  • key (str) – Key for this response

  • response (ClientResponse) – Response to save

class aiohttp_client_cache.backends.base.DictCache(**kwargs)[source]

Bases: aiohttp_client_cache.backends.base.BaseCache, collections.UserDict

Simple in-memory storage that wraps a dict with the BaseStorage interface

_abc_impl = <_abc_data object>
async clear()[source]

Delete all items from the cache

async contains(key)[source]

Check if a key is stored in the cache

Return type

bool

Parameters

key (str) –

async delete(key)[source]

Delete a single item from the cache. Does not raise an error if the item is missing.

Parameters

key (str) –

async keys()[source]

Get all keys stored in the cache

Return type

Iterable[str]

async read(key)[source]

Read a single item from the cache. Returns None if the item is missing.

Return type

Union[CachedResponse, str]

Parameters

key (str) –

async size()[source]

Get the number of items in the cache

Return type

int

async values()[source]

Get all values stored in the cache

Return type

Iterable[Union[CachedResponse, None, bytes, str]]

async write(key, item)[source]

Write an item to the cache

Parameters
aiohttp_client_cache.backends.base._encode_dict(d)[source]