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
(request, response, cache_timeout=None, key_prefix=None, cache=None)
| 398 | |
| 399 | |
| 400 | def learn_cache_key(request, response, cache_timeout=None, key_prefix=None, cache=None): |
| 401 | """ |
| 402 | Learn what headers to take into account for some request URL from the |
| 403 | response object. Store those headers in a global URL registry so that |
| 404 | later access to that URL will know what headers to take into account |
| 405 | without building the response object itself. The headers are named in the |
| 406 | Vary header of the response, but we want to prevent response generation. |
| 407 | |
| 408 | The list of headers to use for cache key generation is stored in the same |
| 409 | cache as the pages themselves. If the cache ages some data out of the |
| 410 | cache, this just means that we have to build the response once to get at |
| 411 | the Vary header and so at the list of headers to use for the cache key. |
| 412 | """ |
| 413 | if key_prefix is None: |
| 414 | key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX |
| 415 | if cache_timeout is None: |
| 416 | cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS |
| 417 | cache_key = _generate_cache_header_key(key_prefix, request) |
| 418 | if cache is None: |
| 419 | cache = caches[settings.CACHE_MIDDLEWARE_ALIAS] |
| 420 | if response.has_header("Vary"): |
| 421 | is_accept_language_redundant = settings.USE_I18N |
| 422 | # If i18n is used, the generated cache key will be suffixed with the |
| 423 | # current locale. Adding the raw value of Accept-Language is redundant |
| 424 | # in that case and would result in storing the same content under |
| 425 | # multiple keys in the cache. See #18191 for details. |
| 426 | headerlist = [] |
| 427 | for header in cc_delim_re.split(response.headers["Vary"]): |
| 428 | header = header.upper().replace("-", "_") |
| 429 | if header != "ACCEPT_LANGUAGE" or not is_accept_language_redundant: |
| 430 | headerlist.append("HTTP_" + header) |
| 431 | headerlist.sort() |
| 432 | cache.set(cache_key, headerlist, cache_timeout) |
| 433 | return _generate_cache_key(request, request.method, headerlist, key_prefix) |
| 434 | else: |
| 435 | # if there is no Vary header, we still need a cache key |
| 436 | # for the request.build_absolute_uri() |
| 437 | cache.set(cache_key, [], cache_timeout) |
| 438 | return _generate_cache_key(request, request.method, [], key_prefix) |
| 439 | |
| 440 | |
| 441 | def _to_tuple(s): |