Cache Keys

Internal utilities for generating cache keys that are used for request matching

create_key

Create a normalized cache key based on a request object

normalize_body

Normalize and filter a request body if possible, depending on Content-Type

normalize_headers

Sort and filter request headers, and normalize minor variations in multi-value headers

normalize_request

Normalize and remove ignored parameters from request URL, body, and headers.

normalize_params

Normalize and filter urlencoded params from either a URL or request body with form data

normalize_url

Normalize and filter a URL.

requests_cache.cache_keys.create_key(request, ignored_parameters=None, match_headers=False, serializer=None, content_root_key=None, **request_kwargs)[source]

Create a normalized cache key based on a request object

Parameters:
  • request (Union[Request, PreparedRequest, CachedRequest]) – Request object to generate a cache key from

  • ignored_parameters (Optional[Iterable[str]]) – Request parameters, headers, and/or JSON body params to exclude

  • match_headers (Union[Iterable[str], None, bool]) – Match only the specified headers, or True to match all headers

  • serializer (Any) – Serializer name or instance

  • content_root_key (Optional[str]) – root element in the request body to apply ignored_parameters to

  • request_kwargs – Additional keyword arguments for request()

Return type:

str

requests_cache.cache_keys.filter_sort_dict(data, ignored_parameters=None)[source]
Parameters:
Return type:

Dict[str, str]

requests_cache.cache_keys.filter_sort_json(data, ignored_parameters)[source]
Parameters:
requests_cache.cache_keys.filter_sort_list(data, ignored_parameters=None)[source]
Parameters:
Return type:

List

requests_cache.cache_keys.filter_sort_multidict(data, ignored_parameters=None)[source]
Parameters:
Return type:

List[Tuple[str, str]]

requests_cache.cache_keys.filter_url(url, ignored_parameters)[source]

Filter ignored parameters out of a URL

Parameters:
Return type:

str

requests_cache.cache_keys.get_matched_headers(headers, match_headers)[source]

Get only the headers we should match against as a list of k=v strings, given an optional include list.

Parameters:
Return type:

List[str]

requests_cache.cache_keys.normalize_body(request, ignored_parameters, content_root_key=None)[source]

Normalize and filter a request body if possible, depending on Content-Type

Parameters:
Return type:

bytes

requests_cache.cache_keys.normalize_headers(headers, ignored_parameters=None)[source]

Sort and filter request headers, and normalize minor variations in multi-value headers

Parameters:
Return type:

CaseInsensitiveDict

requests_cache.cache_keys.normalize_json_body(original_body, ignored_parameters, content_root_key=None)[source]

Normalize and filter a request body with serialized JSON data

Parameters:
Return type:

Union[str, bytes]

requests_cache.cache_keys.normalize_params(value, ignored_parameters=None)[source]

Normalize and filter urlencoded params from either a URL or request body with form data

Parameters:
Return type:

str

requests_cache.cache_keys.normalize_request(request, ignored_parameters=None, content_root_key=None)[source]

Normalize and remove ignored parameters from request URL, body, and headers. This is used for both:

  • Increasing cache hits by generating more precise cache keys

  • Redacting potentially sensitive info from cached requests

Parameters:
Return type:

Union[PreparedRequest, CachedRequest]

requests_cache.cache_keys.normalize_url(url, ignored_parameters)[source]

Normalize and filter a URL. This includes request parameters, IDN domains, scheme, host, port, etc.

Parameters:
Return type:

str

requests_cache.cache_keys.redact_response(response, ignored_parameters)[source]

Redact any ignored parameters (potentially containing sensitive info) from a cached request

Parameters:
Return type:

CachedResponse