Fixed #20346 -- Made cache middleware vary on the full URL.

Previously, only the URL path was included in the cache key.

Thanks jamey for the suggestion.
This commit is contained in:
ijl 2013-12-26 23:14:08 -05:00 committed by Tim Graham
parent 280c1a65cc
commit 71a03e01aa
4 changed files with 92 additions and 19 deletions

View file

@ -622,6 +622,19 @@ cache-specific errors. This has been fixed in Django 1.7, see
.. _Ticket #21200: https://code.djangoproject.com/ticket/21200
Cache keys are now generated from the request's absolute URL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previous versions of Django generated cache keys using a request's path and
query string but not the scheme or host. If a Django application was serving
multiple subdomains or domains, cache keys could collide. In Django 1.7, cache
keys vary by the absolute URL of the request including scheme, host, path, and
query string. For example, the URL portion of a cache key is now generated from
``http://www.example.com/path/to/?key=val`` rather than ``/path/to/?key=val``.
The cache keys generated by Django 1.7 will be different from the keys
generated by older versions of Django. After upgrading to Django 1.7, the first
request to any previously cached URL will be a cache miss .
Passing ``None`` to ``Manager.db_manager()``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -1046,13 +1046,19 @@ the contents of a Web page depend on a user's language preference, the page is
said to "vary on language."
By default, Django's cache system creates its cache keys using the requested
path and query -- e.g., ``"/stories/2005/?order_by=author"``. This means every
fully-qualified URL -- e.g.,
``"http://www.example.com/stories/2005/?order_by=author"``. This means every
request to that URL will use the same cached version, regardless of user-agent
differences such as cookies or language preferences. However, if this page
produces different content based on some difference in request headers -- such
as a cookie, or a language, or a user-agent -- you'll need to use the ``Vary``
header to tell caching mechanisms that the page output depends on those things.
.. versionchanged:: 1.7
Cache keys use the request's fully-qualified URL rather than path
and query string.
To do this in Django, use the convenient
:func:`django.views.decorators.vary.vary_on_headers` view decorator, like so::