cache_control¶
Summary¶
|
A dataclass that contains info on specific actions to take for a given cache item. |
|
Get the first non- |
|
Get all Cache-Control directives, and handle multiple headers and comma-separated lists |
|
Convert an expiration value in any supported format to an absolute datetime |
|
Check for a matching per-URL expiration, if any |
|
Determine if headers contain cache directives that we currently support |
|
Attempt to parse an HTTP (RFC 5322-compatible) timestamp |
|
Split a cache directive into a |
|
“All internal datetimes are UTC and timezone-naive. |
|
Convert a string value to an int, if possible, otherwise |
|
Determine if a URL matches a pattern |
Module Contents¶
Utilities for determining cache expiration and other cache actions
-
class
CacheActions
(key=None, expire_after=None, skip_read=False, skip_write=False, revalidate=False)¶ Bases:
object
A dataclass that contains info on specific actions to take for a given cache item. This is determined by a combination of CacheBackend settings and request + response headers. If multiple sources are provided, they will be used in the following order of precedence:
Cache-Control request headers (if enabled)
Cache-Control response headers (if enabled)
Per-request expiration
Per-URL expiration
Per-session expiration
-
expire_after
¶
-
property
expires
¶ Convert the user/header-provided expiration value to a datetime
-
classmethod
from_headers
(key, headers)¶ Initialize from request headers
- Parameters
key (str) –
headers (Mapping) –
-
classmethod
from_request
(key, cache_control=False, headers=None, **kwargs)¶ Initialize from request info and CacheBackend settings
-
classmethod
from_settings
(key, url, request_expire_after=None, session_expire_after=None, urls_expire_after=None, **kwargs)¶ Initialize from CacheBackend settings
- Parameters
key (str) –
url (Union[str, yarl.URL]) –
request_expire_after (Union[None, int, float, str, datetime.datetime, datetime.timedelta]) –
session_expire_after (Union[None, int, float, str, datetime.datetime, datetime.timedelta]) –
urls_expire_after (Optional[Dict[str, Union[None, int, float, str, datetime.datetime, datetime.timedelta]]]) –
-
key
¶
-
revalidate
¶
-
skip_read
¶
-
skip_write
¶
-
update_from_response
(response)¶ Update expiration + actions based on response headers, if not previously set by request
- Parameters
response (aiohttp.client_reqrep.ClientResponse) –
-
coalesce
(*values, default=None)¶ Get the first non-
None
value in a list of values- Return type
- Parameters
values (Any) –
-
get_cache_directives
(headers)¶ Get all Cache-Control directives, and handle multiple headers and comma-separated lists
- Return type
- Parameters
headers (Mapping) –
-
get_expiration_datetime
(expire_after)¶ Convert an expiration value in any supported format to an absolute datetime
- Return type
- Parameters
expire_after (Union[None, int, float, str, datetime.datetime, datetime.timedelta]) –
-
get_url_expiration
(url, urls_expire_after=None)¶ Check for a matching per-URL expiration, if any
-
has_cache_headers
(headers)¶ Determine if headers contain cache directives that we currently support
- Return type
- Parameters
headers (Mapping) –
-
parse_http_date
(value)¶ Attempt to parse an HTTP (RFC 5322-compatible) timestamp
-
split_kv_directive
(header_value)¶ Split a cache directive into a
(header_value, int)
key-value pair, if possible; otherwise just(header_value, True)
.
-
to_utc
(dt)¶ “All internal datetimes are UTC and timezone-naive. Convert any user/header-provided datetimes to the same format.
- Parameters
dt (datetime.datetime) –
-
try_int
(value)¶ Convert a string value to an int, if possible, otherwise
None
-
url_match
(url, pattern)¶ Determine if a URL matches a pattern
- Parameters
- Return type
Example
>>> url_match('https://httpbin.org/delay/1', 'httpbin.org/delay') True >>> url_match('https://httpbin.org/stream/1', 'httpbin.org/*/1') True >>> url_match('https://httpbin.org/stream/2', 'httpbin.org/*/1') False