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
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) –
-
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
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
key (str) –
item (Union[aiohttp_client_cache.response.CachedResponse, None, bytes, str]) –
-
-
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 ofBaseCache
to use asCacheBackend.responses
andCacheBackend.response_aliases
.- Parameters
cache_name (
str
) – Cache prefix or namespace, depending on backend; see notes belowexpire_after (
Union
[int
,timedelta
,None
]) – Number of hours after which a cache entry will expire; seNone
to never expireallowed_codes (
tuple
) – Limit caching only for response with this codesallowed_methods (
tuple
) – Cache only requests of this methodsinclude_headers (
bool
) – Make request headers part of the cache keyignored_params (
Optional
[Iterable
]) – List of request parameters to be excluded from the cache key.filter_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 responses
The
cache_name
parameter will be used as follows depending on the backend:sqlite
: Cache filename prefix, e.gmy_cache.sqlite
mongodb
: Database nameredis
: 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 provideignored_parameters
to ignore specific request params. This is useful, for example, when requesting the same resource with different credentials or access tokens.-
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 tofilter_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]]) –
-
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
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
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
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
key (str) –
item (Union[aiohttp_client_cache.response.CachedResponse, None, bytes, str]) –
-