redis¶
Summary¶
|
|
|
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)
- Parameters
cache_name (str) –
expire_after (Union[None, int, float, str, datetime.datetime, datetime.timedelta]) –
urls_expire_after (Dict[str, Union[None, int, float, str, datetime.datetime, datetime.timedelta]]) –
allowed_codes (tuple) –
allowed_methods (tuple) –
include_headers (bool) –
ignored_params (Iterable) –
cache_control (bool) –
filter_fn (Callable) –
db (int) –
ssl (ssl.SSLContext) –
encoding (str) –
commands_factory (Callable) –
minsize (int) –
maxsize (int) –
parser (Callable) –
timeout (float) –
- __init__(cache_name='aiohttp-cache', address='redis://localhost', **kwargs)¶
- Parameters
cache_name (
str
) – Cache prefix or namespace, depending on backendexpire_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 responsessecret_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
anddumps
methodsaddress (
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
aioredis.abc.AbcConnection
interface and returning an instance providing high-level interface to Redisminsize (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
aioredis.abc.AbcPool
.connection_cls – Can be used to make pool instantiate custom connection classes. This argument must be a subclass of
aioredis.abc.AbcConnection
.loop – An optional event loop instance (uses
asyncio.get_event_loop()
if not specified).
- 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
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 close()¶
- 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
- keys()¶
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.
- values()¶
Get all values stored in the cache
- Return type
- async write(key, item)¶
Write an item to the cache
- Parameters
key (str) –
item (Optional[Union[aiohttp_client_cache.response.CachedResponse, bytes, str]]) –