redis#

Summary#

RedisBackend([cache_name, address])

Async cache backend for Redis (requires redis-py)

RedisCache(namespace, collection_name[, ...])

An async interface for caching objects in Redis.

Module Contents#

class RedisBackend(cache_name='aiohttp-cache', address='redis://localhost', **kwargs)#

Bases: aiohttp_client_cache.backends.base.CacheBackend

Async cache backend for Redis (requires redis-py)

__init__(cache_name='aiohttp-cache', address='redis://localhost', **kwargs)#
Parameters
  • cache_name (str) – Used as a namespace (prefix for hash key)

  • address (str) – Redis server URI

  • expire_after (Union[None, int, float, str, datetime.datetime, datetime.timedelta]) – Time after which a cache entry will be expired; see Cache Expiration for possible formats

  • urls_expire_after (Optional[Dict[str, Union[None, int, float, str, datetime.datetime, datetime.timedelta]]]) – Expiration times to apply for different URL patterns

  • allowed_codes (tuple) – Only cache responses with these status codes

  • allowed_methods (tuple) – Only cache requests with these HTTP methods

  • include_headers (bool) – Cache requests with different headers separately

  • ignored_params (Optional[Iterable]) – Request parameters to be excluded from the cache key

  • cache_control (bool) – Use Cache-Control response headers

  • 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

  • secret_key (Optional[Union[Iterable, str, bytes]]) – Optional secret key used to sign cache items for added security

  • salt (Union[str, bytes]) – Optional salt used to sign cache items

  • serializer – Custom serializer that provides loads and dumps methods

  • db (Union[str, int]) – Redis database index to switch to when connected

  • username (Optional[str]) – Username to use if Redis server instance requires authorization

  • password (Optional[str]) – Password to use if Redis server instance requires authorization

  • decode_responses (bool) – Enable response decoding

  • encoding (str) – Codec to use for response decoding

  • socket_timeout (Optional[float]) – Timeout for a dropped connection, in seconds

  • socket_connect_timeout (Optional[float]) – Timeout for a initial connection, in seconds

  • retry_on_timeout (bool) – Retry when the connection times out

  • socket_keepalive (bool) –

  • socket_keepalive_options (Optional[Mapping[int, Union[int, bytes]]]) –

  • socket_type (int) –

  • encoding_errors (str) –

  • socket_read_size (int) –

  • health_check_interval (float) –

  • client_name (Optional[str]) –

Parameters
async close()#

Close any active connections

class RedisCache(namespace, collection_name, address='redis://localhost', connection=None, **kwargs)#

Bases: aiohttp_client_cache.backends.base.BaseCache

An async interface for caching objects in Redis.

Parameters
  • namespace (str) – namespace to use

  • collection_name (str) – name of the hash map stored in redis

  • connection (Optional[Redis]) – An existing connection object to reuse instead of creating a new one

  • address (str) – Address of Redis server

  • kwargs – Additional keyword arguments for aioredis.Redis

Note: The hash key name on the redis server will be namespace:collection_name.

_abc_impl = <_abc_data object>#
async bulk_delete(keys)#

Requires redis version >=2.4

Parameters

keys (set) –

async clear()#

Delete all items from the cache

async close()#
async contains(key)#

Check if a key is stored in the cache

Parameters

key (str) –

Return type

bool

async delete(key)#

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

Parameters

key (str) –

async get_connection()#

Lazy-initialize redis connection

async keys()#

Get all keys stored in the cache

Return type

AsyncIterable[str]

async read(key)#

Read an item from the cache. Returns None if the item is missing.

Parameters

key (str) –

Return type

Union[CachedResponse, bytes, str, None]

async size()#

Get the number of items in the cache

Return type

int

async values()#

Get all values stored in the cache

Return type

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

async write(key, item)#

Write an item to the cache

Parameters