Baseο
Base interface for implementations of all cache backends.
Classes:
|
Settings for a response type from a given endpoint to be used to configure a repository in the cache backend. |
|
Represents a repository in the backend cache, providing a dict-like interface for interacting with this repository. |
|
Represents a backend cache of many repositories, providing a dict-like interface for interacting with them. |
- class aiorequestful.cache.backend.base.ResponseRepositorySettings(name, payload_handler=<aiorequestful.response.payload.StringPayloadHandler object>)ο
Bases:
GenericSettings for a response type from a given endpoint to be used to configure a repository in the cache backend.
Attributes:
That name of the repository in the backend
payload_handler(response)Handles payload data conversion to/from expected format for de/serialization.
The names of the fields relating to the keys extracted by
get_key()in the order in which they appear from the results of this method.Methods:
get_key(**kwargs)Extracts the name to assign to a cache entry in the repository from the given request kwargs.
get_name(payload)Extracts the name to assign to a cache entry in the repository from a given
response.-
name:
strο That name of the repository in the backend
-
payload_handler(response: str | bytes | bytearray | aiohttp.client_reqrep.ClientResponse | T) collections.abc.Awaitable:
PayloadHandler[TypeVar(V)] = <aiorequestful.response.payload.StringPayloadHandler object>ο Handles payload data conversion to/from expected format for de/serialization.
- abstract property fields: tuple[str, ...]ο
The names of the fields relating to the keys extracted by
get_key()in the order in which they appear from the results of this method.
- abstractmethod get_key(**kwargs)ο
Extracts the name to assign to a cache entry in the repository from the given request kwargs.
See aiohttp reference for more info on available kwargs: https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ClientSession.request
- Return type:
tuple
- abstractmethod static get_name(payload)ο
Extracts the name to assign to a cache entry in the repository from a given
response.- Return type:
str|None
-
name:
- class aiorequestful.cache.backend.base.ResponseRepository(settings, expire=datetime.timedelta(days=7))ο
Bases:
AsyncIterable[tuple[K,V]],GenericRepresents a repository in the backend cache, providing a dict-like interface for interacting with this repository.
A repository is a data store within the backend e.g. a table in a database.
- Parameters:
settings (
ResponseRepositorySettings[TypeVar(V, bound=Any)]) β The settings to use to identify and interact with the repository in the backend.expire (
timedelta|relativedelta(default:datetime.timedelta(days=7))) β The expiry time to apply to cached responses after which responses are invalidated.
Attributes:
The datetime representing the maximum allowed expiry time from now.
The
logging.Loggerfor this objectThe settings to use to identify and interact with the repository in the backend.
The current connection to the backend.
Methods:
create(*args,Β **kwargs)Set up the backend repository in the backend database if it doesn't already exist and return the initialised object that represents it.
commit()Commit the changes to the repository
close()Close the connection to the repository.
count([include_expired])Get the number of responses in this repository.
contains(request)Check whether the repository contains a given
requestclear([expired_only])Clear the repository of all entries.
serialize(value)Serialize a given
valueto a type that can be persisted to the repository.deserialize(value)Deserialize a value from the repository to the expected response value type.
get_key_from_request(request)Extract the key to use when persisting responses for a given
requestget_response(request)Get the response relating to the given
requestfrom this repository if it exists.get_responses(requests)Get the responses relating to the given
requestsfrom this repository if they exist.save_response(response)Save the given
responseto this repository if a key can be extracted from it.save_responses(responses)Save the given
responsesto this repository if a key can be extracted from them.delete_response(request)Delete the given
requestfrom this repository if it exists.delete_responses(requests)Delete the given
requestsfrom this repository if they exist.- property expire: datetimeο
The datetime representing the maximum allowed expiry time from now.
- abstractmethod classmethod create(*args, **kwargs)ο
Set up the backend repository in the backend database if it doesnβt already exist and return the initialised object that represents it.
- Return type:
Self
-
logger:
Loggerο The
logging.Loggerfor this object
- settingsο
The settings to use to identify and interact with the repository in the backend.
- connectionο
The current connection to the backend.
- abstractmethod async commit()ο
Commit the changes to the repository
- Return type:
None
- abstractmethod async close()ο
Close the connection to the repository.
- Return type:
None
- abstractmethod async count(include_expired=True)ο
Get the number of responses in this repository.
- Parameters:
include_expired (
bool(default:True)) β Whether to include expired responses in the final count.- Returns:
intβ The number of responses in this repository.
- abstractmethod async contains(request)ο
Check whether the repository contains a given
request- Return type:
bool
- abstractmethod async clear(expired_only=False)ο
Clear the repository of all entries.
- Parameters:
expired_only (
bool(default:False)) β Whether to only remove responses that have expired.- Returns:
intβ The number of responses cleared from the repository.
- async serialize(value)ο
Serialize a given
valueto a type that can be persisted to the repository.- Returns:
Optional[TypeVar(V, bound=Any)] β Serialized object if serializing is possible, None otherwise.
- async deserialize(value)ο
Deserialize a value from the repository to the expected response value type.
- Returns:
Anyβ Deserialized object if deserializing is possible, None otherwise.
- abstractmethod get_key_from_request(request)ο
Extract the key to use when persisting responses for a given
request- Return type:
TypeVar(K, bound=tuple)
- abstractmethod async get_response(request)ο
Get the response relating to the given
requestfrom this repository if it exists.- Returns:
Optional[TypeVar(V, bound=Any)] β The result if found.
- async get_responses(requests)ο
Get the responses relating to the given
requestsfrom this repository if they exist.- Returns:
list[TypeVar(V, bound=Any)] β Results unordered.
- async save_response(response)ο
Save the given
responseto this repository if a key can be extracted from it. Safely fail if not- Return type:
None
- async save_responses(responses)ο
Save the given
responsesto this repository if a key can be extracted from them. Safely fail on those that canβt.- Return type:
None
- abstractmethod async delete_response(request)ο
Delete the given
requestfrom this repository if it exists.- Returns:
boolβ True if deleted in the repository and False ifrequestwas not found in the repository.
- async delete_responses(requests)ο
Delete the given
requestsfrom this repository if they exist.- Returns:
intβ The number of the givenrequestsdeleted in the repository.
- class aiorequestful.cache.backend.base.ResponseCache(cache_name, repository_getter=None, expire=datetime.timedelta(days=7))ο
Bases:
MutableMapping[str,R],GenericRepresents a backend cache of many repositories, providing a dict-like interface for interacting with them.
- Parameters:
cache_name (
str) β The name to give to this cache.repository_getter (
Callable[[Self,str|URL],Optional[TypeVar(R, bound=ResponseRepository)]] (default:None)) β A function that can be used to identify the repository in this cache that matches a given URL.expire (
timedelta|relativedelta(default:datetime.timedelta(days=7))) β The expiry time to apply to cached responses after which responses are invalidated.
Attributes:
The name to give to this cache.
A function that can be used to identify the repository in this cache that matches a given URL.
The expiry time to apply to cached responses after which responses are invalidated.
Methods:
connect(value,Β **kwargs)Connect to the backend from a given generic
value.commit()Commit the changes to the cache
close()Close the connection to the repository.
create_repository(settings)Create and return a
SQLiteResponseStorageand store this object in this cache.Returns the repository to use from the stored repositories in this cache for the given
url.get_repository_from_requests(requests)Returns the repository to use from the stored repositories in this cache for the given
requests.get_response(request)Get the response relating to the given
requestfrom the appropriate repository if it exists.get_responses(requests)Get the responses relating to the given
requestsfrom the appropriate repository if they exist.save_response(response)Save the given
responseto the appropriate repository if a key can be extracted from it.save_responses(responses)Save the given
responsesto the appropriate repository if a key can be extracted from them.delete_response(request)Delete the given
requestfrom the appropriate repository if it exists.delete_responses(requests)Delete the given
requestsfrom the appropriate repository.- type = Noneο
- abstractmethod async classmethod connect(value, **kwargs)ο
Connect to the backend from a given generic
value.- Return type:
Self
- cache_nameο
The name to give to this cache.
- repository_getterο
A function that can be used to identify the repository in this cache that matches a given URL.
- expireο
The expiry time to apply to cached responses after which responses are invalidated.
- abstractmethod async commit()ο
Commit the changes to the cache
- Return type:
None
- abstractmethod async close()ο
Close the connection to the repository.
- abstractmethod create_repository(settings)ο
Create and return a
SQLiteResponseStorageand store this object in this cache.Creates a repository with the given
settingsin the cache if it doesnβt exist.- Return type:
- get_repository_from_url(url)ο
Returns the repository to use from the stored repositories in this cache for the given
url.- Return type:
Optional[TypeVar(R, bound=ResponseRepository)]
- get_repository_from_requests(requests)ο
Returns the repository to use from the stored repositories in this cache for the given
requests.- Return type:
Optional[TypeVar(R, bound=ResponseRepository)]
- async get_response(request)ο
Get the response relating to the given
requestfrom the appropriate repository if it exists.- Returns:
Anyβ The result if found.
- async get_responses(requests)ο
Get the responses relating to the given
requestsfrom the appropriate repository if they exist.- Returns:
listβ Results unordered.
- async save_response(response)ο
Save the given
responseto the appropriate repository if a key can be extracted from it.- Return type:
None
- async save_responses(responses)ο
Save the given
responsesto the appropriate repository if a key can be extracted from them. Safely fail on those that canβt.- Return type:
None
- async delete_response(request)ο
Delete the given
requestfrom the appropriate repository if it exists.- Returns:
boolβ True if deleted in the repository and False ifrequestwas not found in the repository.
- async delete_responses(requests)ο
Delete the given
requestsfrom the appropriate repository.- Returns:
intβ The number of the givenrequestsdeleted in the repository.