Cache Control

Internal utilities for determining cache expiration and other cache actions.

CacheActions

A class that translates cache settings and headers into specific actions to take for a given cache item.

class requests_cache.cache_control.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:

  1. Cache-Control request headers

  2. Cache-Control response headers (if enabled)

  3. Per-request expiration

  4. Per-URL expiration

  5. 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

Return type

Optional[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
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