Actions#
Internal utilities for determining cache expiration and other cache actions.
- class requests_cache.policy.actions.CacheActions(cache_control=False, cache_key=None, expire_after=None, request_directives=NOTHING, skip_read=False, skip_write=False, validation_headers=NOTHING)[source]#
Bases:
object
A class that translates cache settings and headers into specific actions to take for a given cache item. Actions include:
Read from the cache
Write to the cache
Set cache expiration
Add headers for conditional requests
If multiple sources provide an expiration time, they will be used in the following order of precedence:
Cache-Control request headers
Cache-Control response headers (if enabled)
Per-request expiration
Per-URL expiration
Per-session expiration
See Cache Headers for more details about behavior.
- 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, session_expire_after=None, urls_expire_after=None, request_expire_after=None, **kwargs)[source]#
Initialize from request info and cache settings
- Parameters
cache_key (
str
) –request (
PreparedRequest
) –cache_control (
bool
) –session_expire_after (
Union
[None
,int
,float
,str
,datetime
,timedelta
]) –urls_expire_after (
Optional
[Dict
[str
,Union
[None
,int
,float
,str
,datetime
,timedelta
]]]) –request_expire_after (
Union
[None
,int
,float
,str
,datetime
,timedelta
]) –
- request_directives#
- skip_read#
- skip_write#
- update_from_cached_response(response)[source]#
Check for relevant cache headers from a cached response, and set headers for a conditional request, if possible.
Used after fetching a cached response, but before potentially sending a new request (if expired).
- Parameters
response (CachedResponse) –
- update_from_response(response)[source]#
Update expiration + actions based on headers from a new response.
Used after receiving a new response but before saving it to the cache.
- Parameters
response (
Response
) –
- validation_headers#
- requests_cache.policy.actions.get_cache_directives(headers)[source]#
Get all Cache-Control directives, and handle multiple headers and comma-separated lists
- requests_cache.policy.actions.get_expiration_datetime(expire_after, ignore_invalid_httpdate=False)[source]#
Convert an expiration value in any supported format to an absolute datetime
- requests_cache.policy.actions.get_expiration_seconds(expire_after)[source]#
Convert an expiration value in any supported format to an expiration time in seconds