cache_control¶
Summary¶
|
A dataclass that contains info on specific actions to take for a given cache item. |
|
Get the first non- |
|
Returns headers containing directives for conditional requests if the cached headers support it |
All internal datetimes are UTC and timezone-naive. |
|
|
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 |
|
|
|
Determine if a URL matches a pattern |
|
Get the current time in UTC, as a timezone-naive datetime |
Module Contents¶
Utilities for determining cache expiration and other cache actions
- class CacheActions(cache_control=False, expire_after=None, key=None, revalidate=False, skip_read=False, skip_write=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
- Parameters:
- classmethod from_headers(key, headers)¶
Initialize from request headers
- classmethod from_request(key, cache_control=False, cache_disabled=False, refresh=False, headers=None, **kwargs)¶
Initialize from request info and CacheBackend settings
- classmethod from_settings(key, url, cache_control=False, refresh=False, request_expire_after=None, session_expire_after=None, urls_expire_after=None, **kwargs)¶
Initialize from CacheBackend settings
- update_from_response(response)¶
Update expiration + actions based on response headers, if not previously set by request
- Parameters:
response (
ClientResponse
)
- coalesce(*values, default=None)¶
Get the first non-
None
value in a list of values
- compose_refresh_headers(request_headers, cached_headers)¶
Returns headers containing directives for conditional requests if the cached headers support it
- convert_to_utc_naive(dt)¶
All internal datetimes are UTC and timezone-naive. Convert any user/header-provided datetimes to the same format.
- Parameters:
dt (
datetime
)
- get_cache_directives(headers)¶
Get all Cache-Control directives, and handle multiple headers and comma-separated lists
- get_expiration_datetime(expire_after)¶
Convert an expiration value in any supported format to an absolute datetime
- 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
- 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)
.
- try_int(value)¶
- 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