Actions#
- class requests_cache.policy.actions.CacheActions(cache_key=None, error_504=False, expire_after=None, send_request=False, resend_request=False, resend_async=False, skip_read=False, skip_write=False, directives=None, settings=None, only_if_cached=False, refresh=False, request=None, stale_if_error=None, stale_while_revalidate=None, validation_headers=_Nothing.NOTHING)[source]#
Bases:
RichMixin
- Translates cache settings and headers into specific actions to take for a given cache item.
The resulting actions are then handled in
CachedSession.send()
.
Notes
See Expiration Precedence for behavior if multiple sources provide an expiration
See Cache Headers for more details about header behavior
The following arguments/properties are the outputs of this class:
- Parameters:
cache_key (
Optional
[str
]) – The cache key created based on the initial requesterror_504 (
bool
) – Indicates the request cannot be fulfilled based on cache settingsexpire_after (
Union
[None
,int
,float
,str
,datetime
,timedelta
]) – User or header-provided expiration valuesend_request (
bool
) – Send a new requestresend_request (
bool
) – Send a new request to refresh a stale cache itemresend_async (
bool
) – Return a stale cache item, and send a non-blocking request to refresh itskip_read (
bool
) – Skip reading from the cacheskip_write (
bool
) – Skip writing to the cachedirectives (
Optional
[CacheDirectives
]) –settings (
Optional
[CacheSettings
]) –only_if_cached (
bool
) –refresh (
bool
) –request (
Optional
[PreparedRequest
]) –stale_if_error (
Union
[bool
,None
,int
,float
,str
,datetime
,timedelta
]) –stale_while_revalidate (
Union
[bool
,None
,int
,float
,str
,datetime
,timedelta
]) –
- property expires: Optional[datetime]#
Convert the user/header-provided expiration value to a datetime. Applies to new cached responses, and previously cached responses that are being revalidated.
- classmethod from_request(cache_key, request, settings=None)[source]#
Initialize from request info and cache settings.
Note on refreshing: must-revalidate isn’t a standard request header, but is used here to indicate a user-requested refresh. Typically that’s only used in response headers, and max-age=0 would be used by a client to request a refresh. However, this would conflict with the expire_after option provided in
CachedSession.request()
.- Parameters:
request (
PreparedRequest
) – The outgoing requestsettings (
Optional
[CacheSettings
]) – Session-level cache settingscache_key (
str
) –
- is_usable(cached_response, error=False)[source]#
Determine whether a given cached response is “fresh enough” to satisfy the request, based on:
min-fresh
max-stale
stale-if-error (if an error has occured)
stale-while-revalidate
- Parameters:
cached_response (
Optional
[CachedResponse
]) –error (
bool
) –
- update_from_cached_response(cached_response, create_key=None, **key_kwargs)[source]#
Determine if we can reuse a cached response, or set headers for a conditional request if possible.
Used after fetching a cached response, but before potentially sending a new request.
- update_from_response(response)[source]#
Update expiration + actions based on headers and other details from a new response.
Used after receiving a new response, but before saving it to the cache.
- Parameters:
response (
Response
) –
- update_request(request)[source]#
Apply validation headers (if any) before sending a request
- Parameters:
request (
PreparedRequest
) –- Return type:
- update_revalidated_response(response, cached_response)[source]#
After revalidation, update the cached response’s expiration and headers
- Parameters:
response (
Response
) –cached_response (
CachedResponse
) –
- Return type: