Cache Keys

Internal utilities for generating the cache keys that are used to match requests

create_key

Create a normalized cache key from either a request object or Request arguments

decode

Decode a value from bytes, if hasn’t already been.

encode

Encode a value to bytes, if it hasn’t already been

get_matched_headers

Get only the headers we should match against, given an optional include list and/or exclude list.

normalize_dict

Sort items in a dict

remove_ignored_body_params

Remove any ignored parameters from the request body

remove_ignored_headers

Remove any ignored request headers

remove_ignored_params

Remove ignored parameters from request URL, body, and headers

remove_ignored_url_params

Remove any ignored request parameters from the URL

sort_dict

param d

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

Create a normalized cache key from either a request object or Request arguments

Parameters
  • request (AnyRequest) –

  • ignored_parameters (Iterable[str]) –

  • match_headers (Union[Iterable[str], bool]) –

Return type

str

requests_cache.cache_keys.decode(value, encoding='utf-8')[source]

Decode a value from bytes, if hasn’t already been. Note: PreparedRequest.body is always encoded in utf-8.

Return type

str

requests_cache.cache_keys.encode(value, encoding='utf-8')[source]

Encode a value to bytes, if it hasn’t already been

Return type

bytes

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

Get only the headers we should match against, given an optional include list and/or exclude list. Also normalizes headers (sorted/lowercased keys).

Parameters
Return type

Dict

requests_cache.cache_keys.normalize_dict(items, normalize_data=True)[source]

Sort items in a dict

Parameters
Return type

Union[Mapping, str, bytes, None]

requests_cache.cache_keys.remove_ignored_body_params(request, ignored_parameters)[source]

Remove any ignored parameters from the request body

Parameters
  • request (AnyRequest) –

  • ignored_parameters (Optional[Iterable[str]]) –

Return type

bytes

requests_cache.cache_keys.remove_ignored_headers(headers, ignored_parameters)[source]

Remove any ignored request headers

Parameters
Return type

CaseInsensitiveDict

requests_cache.cache_keys.remove_ignored_params(request, ignored_parameters)[source]

Remove ignored parameters from request URL, body, and headers

Parameters
  • request (AnyRequest) –

  • ignored_parameters (Optional[Iterable[str]]) –

Return type

AnyRequest

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

Remove any ignored request parameters from the URL

Parameters
Return type

str

requests_cache.cache_keys.sort_dict(d)[source]
Parameters

d (Mapping) –

Return type

Dict