RedisBackend([cache_name, address])

Async cache backend for Redis (requires aioredis)

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 aioredis)

__init__(cache_name='aiohttp-cache', address='redis://localhost', **kwargs)
  • cache_name (str) – Cache prefix or namespace, depending on backend

  • 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 (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 (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 (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

  • address (str) – An address where to connect. Can be a (host, port) tuple, unix domain socket path string, or a Redis URI string.

  • db (int) – Redis database index to switch to when connected.

  • password (Union[str, bytes]) – Password to use if Redis server instance requires authorization.

  • ssl (ssl.SSLContext) – SSL context that is passed through to asyncio.BaseEventLoop.create_connection().

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

  • commands_factory (Callable) – A factory accepting single parameter – object implementing interface and returning an instance providing high-level interface to Redis

  • minsize (int) – Minimum number of connections to initialize and keep in pool.

  • maxsize (int) – Maximum number of connections that can be created in pool.

  • parser (Callable) – Protocol parser class. Can be used to set custom protocol reader; expected same interface as hiredis.Reader.

  • timeout (float) – Max time to open a connection, otherwise raise asyncio.TimeoutError exception.

  • pool_cls – Can be used to instantiate custom pool class. This argument must be a subclass of

  • connection_cls – Can be used to make pool instantiate custom connection classes. This argument must be a subclass of

  • loop – An optional event loop instance (uses asyncio.get_event_loop() if not specified).

class RedisCache(namespace, collection_name, address=None, connection=None, **kwargs)

Bases: aiohttp_client_cache.backends.base.BaseCache

An async interface for caching objects in Redis.

  • 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 (Optional[str]) – Address of Redis server

  • kwargs – Additional keyword arguments for redis.Redis

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

_abc_impl = <_abc_data object>
async clear()

Delete all items from the cache

async contains(key)

Check if a key is stored in the cache

Return type



key (str) –

async delete(key)

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


key (str) –

async get_connection()

Lazy-initialize redis connection


Get all keys stored in the cache

Return type


async read(key)

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

Return type

Union[CachedResponse, bytes, str, None]


key (str) –

async size()

Get the number of items in the cache

Return type



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