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

normalize_request

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

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

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

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

  • ignored_parameters (Optional[Iterable[str]]) – Request parames, headers, and/or body params to not match against

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

  • request_kwargs – Request arguments to generate a cache key from

Return type

str

requests_cache.cache_keys.normalize_request(request, ignored_parameters=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
  • request (Union[Request, PreparedRequest, CachedRequest]) – Request object to normalize

  • ignored_parameters (Optional[Iterable[str]]) – Request parames, headers, and/or body params to not match against and to remove from the request

Return type

Union[PreparedRequest, CachedRequest]