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().


  • 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:

cache_key: str#
error_504: bool#
expire_after: Union[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().

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

  • cached_response (Optional[CachedResponse]) –

  • error (bool) –

resend_async: bool#
resend_request: bool#
send_request: bool#
skip_read: bool#
skip_write: 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.

  • cached_response (Optional[CachedResponse]) – Cached response to examine

  • create_key (Optional[Callable[..., str]]) – Cache key function, used for validating Vary headers

  • key_kwargs – Additional keyword arguments for create_key.


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.


response (Response) –


Apply validation headers (if any) before sending a request


request (PreparedRequest) –

Return type:


update_revalidated_response(response, cached_response)[source]#

After revalidation, update the cached response’s expiration and headers

Return type: