Cache Control¶
Internal utilities for determining cache expiration and other cache actions.
A class that translates cache settings and headers into 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 supported cache directives |
|
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 |
- class requests_cache.cache_control.CacheActions(cache_control=False, cache_key=None, expire_after=None, request_headers=NOTHING, skip_read=False, skip_write=False)[source]¶
Bases:
object
A class that translates cache settings and headers into specific actions to take for a given cache item. Actions include:
Reading from the cache
Writing to the cache
Setting cache expiration
Adding request headers
If multiple sources provide an expiration time, 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
- cache_control¶
- cache_key¶
- expire_after¶
- property expires¶
Convert the user/header-provided expiration value to a datetime
- classmethod from_request(cache_key, request, cache_control=False, **kwargs)[source]¶
Initialize from request info and cache settings
- Parameters
cache_key (
str
) –request (
PreparedRequest
) –cache_control (
bool
) –
- classmethod from_settings(cache_key, url=None, cache_control=True, request_expire_after=None, session_expire_after=None, urls_expire_after=None, **kwargs)[source]¶
Initialize from cache settings
- request_headers¶
- skip_read¶
- skip_write¶
- update_from_cached_response(response)[source]¶
Used after fetching a cached response, but before potentially sending a new request. Check for relevant cache headers on a cached response, and set corresponding request headers.
- Parameters
response (CachedResponse) –
- requests_cache.cache_control.coalesce(*values, default=None)[source]¶
Get the first non-
None
value in a list of values
- requests_cache.cache_control.get_cache_directives(headers)[source]¶
Get all Cache-Control directives, and handle multiple headers and comma-separated lists
- requests_cache.cache_control.get_expiration_datetime(expire_after)[source]¶
Convert an expiration value in any supported format to an absolute datetime
- requests_cache.cache_control.get_url_expiration(url, urls_expire_after=None)[source]¶
Check for a matching per-URL expiration, if any
- requests_cache.cache_control.has_cache_headers(headers)[source]¶
Determine if headers contain supported cache directives
- requests_cache.cache_control.parse_http_date(value)[source]¶
Attempt to parse an HTTP (RFC 5322-compatible) timestamp
- requests_cache.cache_control.split_kv_directive(header_value)[source]¶
Split a cache directive into a
(header_value, int)
key-value pair, if possible; otherwise just(header_value, True)
.
- requests_cache.cache_control.to_utc(dt)[source]¶
All internal datetimes are UTC and timezone-naive. Convert any user/header-provided datetimes to the same format.
- Parameters
dt (
datetime
) –
- requests_cache.cache_control.try_int(value)[source]¶
Convert a string value to an int, if possible, otherwise
None
- requests_cache.cache_control.url_match(url, pattern)[source]¶
Determine if a URL matches a pattern
- Parameters
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
- Return type