module documentation

This module contains helper functions for controlling caching. It does so by managing the "Vary" header of responses. It includes functions to patch the header of response objects directly and decorators that change functions to do that header-patching themselves. For information on the Vary header, see: https://tools.ietf.org/html/rfc7231#section-7.1.4 Essentially, the "Vary" HTTP header defines which headers a cache should take into account when building its cache key. Requests with the same path but different header content for headers named in "Vary" need to get different cache keys to prevent delivery of wrong content. An example: i18n middleware would need to distinguish caches by the "Accept-language" header.

Function add_never_cache_headers Add headers to a response to indicate that a page should never be cached.
Function get_cache_key Return a cache key based on the request URL and query. It can be used in the request phase because it pulls the list of headers to take into account from the global URL registry and uses those to build a cache key to check against.
Function get_conditional_response Undocumented
Function get_max_age Return the max-age from the response Cache-Control header as an integer, or None if it wasn't found or wasn't an integer.
Function has_vary_header Check to see if the response has a given header name in its Vary header.
Function learn_cache_key Learn what headers to take into account for some request URL from the response object. Store those headers in a global URL registry so that later access to that URL will know what headers to take into account without building the response object itself...
Function patch_cache_control Patch the Cache-Control header by adding all keyword arguments to it. The transformation is as follows:
Function patch_response_headers Add HTTP caching headers to the given HttpResponse: Expires and Cache-Control.
Function patch_vary_headers Add (or update) the "Vary" header in the given HttpResponse object. newheaders is a list of header names that should be in "Vary". If headers contains an asterisk, then "Vary" header will consist of a single asterisk '*'...
Function set_response_etag Undocumented
Variable cc_delim_re Undocumented
Function _generate_cache_header_key Return a cache key for the header cache.
Function _generate_cache_key Return a cache key from the headers given in the header list.
Function _i18n_cache_key_suffix If necessary, add the current locale or time zone to the cache key.
Function _if_match_passes Test the If-Match comparison as defined in section 3.1 of RFC 7232.
Function _if_modified_since_passes Test the If-Modified-Since comparison as defined in section 3.3 of RFC 7232.
Function _if_none_match_passes Test the If-None-Match comparison as defined in section 3.2 of RFC 7232.
Function _if_unmodified_since_passes Test the If-Unmodified-Since comparison as defined in section 3.4 of RFC 7232.
Function _not_modified Undocumented
Function _precondition_failed Undocumented
Function _to_tuple Undocumented
def add_never_cache_headers(response): (source)

Add headers to a response to indicate that a page should never be cached.

def get_cache_key(request, key_prefix=None, method='GET', cache=None): (source)

Return a cache key based on the request URL and query. It can be used in the request phase because it pulls the list of headers to take into account from the global URL registry and uses those to build a cache key to check against. If there isn't a headerlist stored, return None, indicating that the page needs to be rebuilt.

def get_conditional_response(request, etag=None, last_modified=None, response=None): (source)

Undocumented

def get_max_age(response): (source)

Return the max-age from the response Cache-Control header as an integer, or None if it wasn't found or wasn't an integer.

def has_vary_header(response, header_query): (source)

Check to see if the response has a given header name in its Vary header.

def learn_cache_key(request, response, cache_timeout=None, key_prefix=None, cache=None): (source)

Learn what headers to take into account for some request URL from the response object. Store those headers in a global URL registry so that later access to that URL will know what headers to take into account without building the response object itself. The headers are named in the Vary header of the response, but we want to prevent response generation. The list of headers to use for cache key generation is stored in the same cache as the pages themselves. If the cache ages some data out of the cache, this just means that we have to build the response once to get at the Vary header and so at the list of headers to use for the cache key.

def patch_cache_control(response, **kwargs): (source)

Patch the Cache-Control header by adding all keyword arguments to it. The transformation is as follows: * All keyword parameter names are turned to lowercase, and underscores are converted to hyphens. * If the value of a parameter is True (exactly True, not just a true value), only the parameter name is added to the header. * All other parameters are added with their value, after applying str() to it.

def patch_response_headers(response, cache_timeout=None): (source)

Add HTTP caching headers to the given HttpResponse: Expires and Cache-Control. Each header is only added if it isn't already set. cache_timeout is in seconds. The CACHE_MIDDLEWARE_SECONDS setting is used by default.

def patch_vary_headers(response, newheaders): (source)

Add (or update) the "Vary" header in the given HttpResponse object. newheaders is a list of header names that should be in "Vary". If headers contains an asterisk, then "Vary" header will consist of a single asterisk '*'. Otherwise, existing headers in "Vary" aren't removed.

def set_response_etag(response): (source)

Undocumented

cc_delim_re = (source)

Undocumented

def _generate_cache_header_key(key_prefix, request): (source)

Return a cache key for the header cache.

def _generate_cache_key(request, method, headerlist, key_prefix): (source)

Return a cache key from the headers given in the header list.

def _i18n_cache_key_suffix(request, cache_key): (source)

If necessary, add the current locale or time zone to the cache key.

def _if_match_passes(target_etag, etags): (source)

Test the If-Match comparison as defined in section 3.1 of RFC 7232.

def _if_modified_since_passes(last_modified, if_modified_since): (source)

Test the If-Modified-Since comparison as defined in section 3.3 of RFC 7232.

def _if_none_match_passes(target_etag, etags): (source)

Test the If-None-Match comparison as defined in section 3.2 of RFC 7232.

def _if_unmodified_since_passes(last_modified, if_unmodified_since): (source)

Test the If-Unmodified-Since comparison as defined in section 3.4 of RFC 7232.

def _not_modified(request, response=None): (source)

Undocumented

def _precondition_failed(request): (source)

Undocumented

def _to_tuple(s): (source)

Undocumented