base¶
Summary¶
|
A wrapper for lower-level cache storage operations. |
|
Base class for cache backends; includes a non-persistent, in-memory cache. |
|
Simple in-memory storage that wraps a dict with the |
Module Contents¶
-
class
BaseCache
(secret_key=None, salt=b'aiohttp-client-cache', serializer=None, **kwargs)¶ 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.-
_abc_impl
= <_abc_data object>¶
-
static
_get_serializer
(secret_key, salt)¶ Get the appropriate serializer to use; either
itsdangerous
, if a secret key is specified, or plainpickle
otherwise. :raises py:exc:ImportError ifsecret_key
is specified butitsdangerous
is not installed:
-
abstract async
clear
()¶ Delete all items from the cache
-
abstract async
contains
(key)¶ Check if a key is stored in the cache
-
abstract async
delete
(key)¶ Delete an item from the cache. Does not raise an error if the item is missing.
- Parameters
key (str) –
-
deserialize
(item)¶ Deserialize a cached URL or response
- Return type
- Parameters
item (Optional[Union[aiohttp_client_cache.response.CachedResponse, bytes, str]]) –
-
abstract
keys
()¶ Get all keys stored in the cache
- Return type
-
async
pop
(key, default=None)¶ Delete an item from the cache, and return the deleted item
-
abstract async
read
(key)¶ Read an item from the cache. Returns
None
if the item is missing.
-
serialize
(item=None)¶ Serialize a URL or response into bytes
- Return type
- Parameters
item (Optional[Union[aiohttp_client_cache.response.CachedResponse, bytes, str]]) –
-
abstract
values
()¶ Get all values stored in the cache
- Return type
-
abstract async
write
(key, item)¶ Write an item to the cache
- Parameters
key (str) –
item (Optional[Union[aiohttp_client_cache.response.CachedResponse, bytes, str]]) –
-
-
class
CacheBackend
(cache_name='aiohttp-cache', expire_after=-1, urls_expire_after=None, allowed_codes=(200, ), allowed_methods=('GET', 'HEAD'), include_headers=False, ignored_params=None, filter_fn=<function CacheBackend.<lambda>>, **kwargs)¶ Bases:
object
Base class for cache backends; includes a non-persistent, in-memory cache.
This manages higher-level cache operations, including cache expiration, generating cache keys, and managing redirect history.
Lower-level storage operations are handled by
BaseCache
. To extend this with your own custom backend, implement one or more subclasses ofBaseCache
to use asCacheBackend.responses
andCacheBackend.response_aliases
.-
__init__
(cache_name='aiohttp-cache', expire_after=-1, urls_expire_after=None, allowed_codes=(200, ), allowed_methods=('GET', 'HEAD'), include_headers=False, ignored_params=None, filter_fn=<function CacheBackend.<lambda>>, **kwargs)¶ - Parameters
cache_name (
str
) – Cache prefix or namespace, depending on backendexpire_after (
Union
[None
,int
,float
,datetime
,timedelta
]) – Time after which a cache entry will be expired. For numeric values, specify either a positive value in seconds, or-1
to never expire.urls_expire_after (
Optional
[Dict
[str
,Union
[None
,int
,float
,datetime
,timedelta
]]]) – Expiration times to apply for different URL patternsallowed_codes (
tuple
) – Only cache responses with these status codesallowed_methods (
tuple
) – Only cache requests with these HTTP methodsinclude_headers (
bool
) – Cache requests with different headers separatelyignored_params (
Optional
[Iterable
]) – Request parameters to be excluded from the cache keyfilter_fn (
Callable
) – function that takes aaiohttp.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
methods
-
_get_expiration
(response, request_expire_after=None)¶ Get the appropriate expiration for the given response
- Return type
- Parameters
response (aiohttp.client_reqrep.ClientResponse) –
request_expire_after (Union[None, int, float, datetime.datetime, datetime.timedelta]) –
-
async
_get_redirect_response
(key)¶ Get the response referenced by a redirect key, if available
- Return type
- Parameters
key (str) –
-
async
clear
()¶ Clear cache
-
create_key
(*args, **kwargs)¶ Create a unique cache key based on request details
-
async
delete
(key)¶ Delete a response from the cache, along with its history (if applicable)
- Parameters
key (str) –
-
async
delete_expired_responses
()¶ Deletes all expired responses from the cache. Also deletes any cache items that are filtered out according to
filter_fn()
.
-
async
delete_url
(url)¶ Delete cached response associated with url, along with its history (if applicable). Works only for GET requests.
- Parameters
url (Union[str, yarl.URL]) –
-
async
get_response
(key)¶ Retrieve response and timestamp for key if it’s stored in cache, otherwise returns
None`
- Parameters
key (
str
) – key of resource- Return type
-
get_urls
()¶ Get all URLs currently in the cache
- Return type
-
async
has_url
(url)¶ Returns True if cache has url, False otherwise. Works only for GET request urls
-
is_cacheable
(response)¶ Perform all checks needed to determine if the given response should be cached
- Return type
- Parameters
response (Optional[Union[aiohttp.client_reqrep.ClientResponse, aiohttp_client_cache.response.CachedResponse]]) –
-
async
save_response
(key, response, expire_after=None)¶ Save response to cache
-
-
class
DictCache
(secret_key=None, salt=b'aiohttp-client-cache', serializer=None, **kwargs)¶ 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
()¶ Delete all items from the cache
-
async
delete
(key)¶ Delete an item from the cache. Does not raise an error if the item is missing.
- Parameters
key (str) –
-
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.- Return type
- Parameters
key (str) –
-
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]]) –
-