2005-10-09 07:19:21 +08:00
|
|
|
"""
|
2005-10-09 08:37:56 +08:00
|
|
|
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.
|
2005-10-09 07:19:21 +08:00
|
|
|
|
|
|
|
For information on the Vary header, see:
|
|
|
|
|
|
|
|
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2007-10-29 06:32:25 +08:00
|
|
|
An example: i18n middleware would need to distinguish caches by the
|
2005-10-09 07:19:21 +08:00
|
|
|
"Accept-language" header.
|
|
|
|
"""
|
|
|
|
|
2011-03-28 10:11:19 +08:00
|
|
|
import hashlib
|
2007-07-16 11:50:22 +08:00
|
|
|
import re
|
|
|
|
import time
|
2007-10-29 06:32:25 +08:00
|
|
|
|
2005-10-09 07:19:21 +08:00
|
|
|
from django.conf import settings
|
2010-12-22 15:52:44 +08:00
|
|
|
from django.core.cache import get_cache
|
2007-09-29 06:34:27 +08:00
|
|
|
from django.utils.encoding import smart_str, iri_to_uri
|
2007-10-31 11:59:40 +08:00
|
|
|
from django.utils.http import http_date
|
2010-03-01 18:19:01 +08:00
|
|
|
from django.utils.translation import get_language
|
2005-10-09 07:19:21 +08:00
|
|
|
|
2005-10-30 01:00:20 +08:00
|
|
|
cc_delim_re = re.compile(r'\s*,\s*')
|
2005-10-31 11:14:57 +08:00
|
|
|
|
2005-10-30 01:00:20 +08:00
|
|
|
def patch_cache_control(response, **kwargs):
|
|
|
|
"""
|
|
|
|
This function patches the Cache-Control header by adding all
|
|
|
|
keyword arguments to it. The transformation is as follows:
|
|
|
|
|
2005-10-31 11:14:57 +08:00
|
|
|
* 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.
|
2005-10-30 01:00:20 +08:00
|
|
|
"""
|
|
|
|
def dictitem(s):
|
2007-10-29 06:32:25 +08:00
|
|
|
t = s.split('=', 1)
|
2005-10-30 01:00:20 +08:00
|
|
|
if len(t) > 1:
|
2007-09-15 05:42:26 +08:00
|
|
|
return (t[0].lower(), t[1])
|
2005-10-30 01:00:20 +08:00
|
|
|
else:
|
2007-09-15 05:42:26 +08:00
|
|
|
return (t[0].lower(), True)
|
2005-10-30 01:00:20 +08:00
|
|
|
|
|
|
|
def dictvalue(t):
|
2007-07-16 11:50:22 +08:00
|
|
|
if t[1] is True:
|
2005-10-30 01:00:20 +08:00
|
|
|
return t[0]
|
|
|
|
else:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return t[0] + '=' + smart_str(t[1])
|
2005-10-30 01:00:20 +08:00
|
|
|
|
|
|
|
if response.has_header('Cache-Control'):
|
|
|
|
cc = cc_delim_re.split(response['Cache-Control'])
|
|
|
|
cc = dict([dictitem(el) for el in cc])
|
|
|
|
else:
|
|
|
|
cc = {}
|
2007-09-29 06:30:59 +08:00
|
|
|
|
|
|
|
# If there's already a max-age header but we're being asked to set a new
|
2008-08-16 00:38:41 +08:00
|
|
|
# max-age, use the minimum of the two ages. In practice this happens when
|
2007-09-29 06:30:59 +08:00
|
|
|
# a decorator and a piece of middleware both operate on a given view.
|
|
|
|
if 'max-age' in cc and 'max_age' in kwargs:
|
|
|
|
kwargs['max_age'] = min(cc['max-age'], kwargs['max_age'])
|
|
|
|
|
2011-08-23 11:51:10 +08:00
|
|
|
# Allow overriding private caching and vice versa
|
|
|
|
if 'private' in cc and 'public' in kwargs:
|
|
|
|
del cc['private']
|
|
|
|
elif 'public' in cc and 'private' in kwargs:
|
|
|
|
del cc['public']
|
|
|
|
|
2007-10-29 06:32:25 +08:00
|
|
|
for (k, v) in kwargs.items():
|
2005-10-30 01:00:20 +08:00
|
|
|
cc[k.replace('_', '-')] = v
|
|
|
|
cc = ', '.join([dictvalue(el) for el in cc.items()])
|
|
|
|
response['Cache-Control'] = cc
|
|
|
|
|
2007-11-30 00:57:18 +08:00
|
|
|
def get_max_age(response):
|
|
|
|
"""
|
|
|
|
Returns the max-age from the response Cache-Control header as an integer
|
|
|
|
(or ``None`` if it wasn't found or wasn't an integer.
|
|
|
|
"""
|
|
|
|
if not response.has_header('Cache-Control'):
|
|
|
|
return
|
|
|
|
cc = dict([_to_tuple(el) for el in
|
|
|
|
cc_delim_re.split(response['Cache-Control'])])
|
|
|
|
if 'max-age' in cc:
|
|
|
|
try:
|
|
|
|
return int(cc['max-age'])
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
pass
|
|
|
|
|
2005-10-09 07:19:21 +08:00
|
|
|
def patch_response_headers(response, cache_timeout=None):
|
|
|
|
"""
|
|
|
|
Adds some useful headers to the given HttpResponse object:
|
|
|
|
ETag, Last-Modified, 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.
|
|
|
|
"""
|
|
|
|
if cache_timeout is None:
|
|
|
|
cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
|
2007-07-16 11:50:22 +08:00
|
|
|
if cache_timeout < 0:
|
|
|
|
cache_timeout = 0 # Can't have max-age negative
|
2010-12-13 06:57:17 +08:00
|
|
|
if settings.USE_ETAGS and not response.has_header('ETag'):
|
2011-03-28 10:11:19 +08:00
|
|
|
response['ETag'] = '"%s"' % hashlib.md5(response.content).hexdigest()
|
2005-10-09 07:19:21 +08:00
|
|
|
if not response.has_header('Last-Modified'):
|
2007-10-31 11:59:40 +08:00
|
|
|
response['Last-Modified'] = http_date()
|
2005-10-09 07:19:21 +08:00
|
|
|
if not response.has_header('Expires'):
|
2007-10-31 11:59:40 +08:00
|
|
|
response['Expires'] = http_date(time.time() + cache_timeout)
|
2005-10-30 01:00:20 +08:00
|
|
|
patch_cache_control(response, max_age=cache_timeout)
|
2005-10-09 07:19:21 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def add_never_cache_headers(response):
|
|
|
|
"""
|
2007-10-29 06:32:25 +08:00
|
|
|
Adds headers to a response to indicate that a page should never be cached.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
patch_response_headers(response, cache_timeout=-1)
|
|
|
|
|
2005-10-09 07:19:21 +08:00
|
|
|
def patch_vary_headers(response, newheaders):
|
|
|
|
"""
|
|
|
|
Adds (or updates) the "Vary" header in the given HttpResponse object.
|
|
|
|
newheaders is a list of header names that should be in "Vary". Existing
|
|
|
|
headers in "Vary" aren't removed.
|
|
|
|
"""
|
|
|
|
# Note that we need to keep the original order intact, because cache
|
|
|
|
# implementations may rely on the order of the Vary contents in, say,
|
|
|
|
# computing an MD5 hash.
|
|
|
|
if response.has_header('Vary'):
|
2007-11-19 12:00:41 +08:00
|
|
|
vary_headers = cc_delim_re.split(response['Vary'])
|
|
|
|
else:
|
|
|
|
vary_headers = []
|
|
|
|
# Use .lower() here so we treat headers as case-insensitive.
|
|
|
|
existing_headers = set([header.lower() for header in vary_headers])
|
|
|
|
additional_headers = [newheader for newheader in newheaders
|
|
|
|
if newheader.lower() not in existing_headers]
|
|
|
|
response['Vary'] = ', '.join(vary_headers + additional_headers)
|
2005-10-09 07:19:21 +08:00
|
|
|
|
2011-02-01 08:20:31 +08:00
|
|
|
def has_vary_header(response, header_query):
|
|
|
|
"""
|
|
|
|
Checks to see if the response has a given header name in its Vary header.
|
|
|
|
"""
|
|
|
|
if not response.has_header('Vary'):
|
|
|
|
return False
|
|
|
|
vary_headers = cc_delim_re.split(response['Vary'])
|
|
|
|
existing_headers = set([header.lower() for header in vary_headers])
|
|
|
|
return header_query.lower() in existing_headers
|
|
|
|
|
2010-03-01 18:19:01 +08:00
|
|
|
def _i18n_cache_key_suffix(request, cache_key):
|
|
|
|
"""If enabled, returns the cache key ending with a locale."""
|
|
|
|
if settings.USE_I18N:
|
|
|
|
# first check if LocaleMiddleware or another middleware added
|
|
|
|
# LANGUAGE_CODE to request, then fall back to the active language
|
|
|
|
# which in turn can also fall back to settings.LANGUAGE_CODE
|
|
|
|
cache_key += '.%s' % getattr(request, 'LANGUAGE_CODE', get_language())
|
|
|
|
return cache_key
|
|
|
|
|
2010-10-29 09:31:15 +08:00
|
|
|
def _generate_cache_key(request, method, headerlist, key_prefix):
|
2007-10-29 06:32:25 +08:00
|
|
|
"""Returns a cache key from the headers given in the header list."""
|
2011-03-28 10:11:19 +08:00
|
|
|
ctx = hashlib.md5()
|
2005-10-09 07:19:21 +08:00
|
|
|
for header in headerlist:
|
|
|
|
value = request.META.get(header, None)
|
|
|
|
if value is not None:
|
|
|
|
ctx.update(value)
|
2011-03-28 10:11:19 +08:00
|
|
|
path = hashlib.md5(iri_to_uri(request.get_full_path()))
|
2010-10-29 09:31:15 +08:00
|
|
|
cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % (
|
|
|
|
key_prefix, request.method, path.hexdigest(), ctx.hexdigest())
|
2010-03-01 18:19:01 +08:00
|
|
|
return _i18n_cache_key_suffix(request, cache_key)
|
2005-10-09 07:19:21 +08:00
|
|
|
|
2009-03-31 06:37:59 +08:00
|
|
|
def _generate_cache_header_key(key_prefix, request):
|
|
|
|
"""Returns a cache key for the header cache."""
|
2011-03-28 10:11:19 +08:00
|
|
|
path = hashlib.md5(iri_to_uri(request.get_full_path()))
|
2010-02-24 04:45:28 +08:00
|
|
|
cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
|
|
|
|
key_prefix, path.hexdigest())
|
2010-03-01 18:19:01 +08:00
|
|
|
return _i18n_cache_key_suffix(request, cache_key)
|
2009-03-31 06:37:59 +08:00
|
|
|
|
2010-12-22 15:52:44 +08:00
|
|
|
def get_cache_key(request, key_prefix=None, method='GET', cache=None):
|
2005-10-09 07:19:21 +08:00
|
|
|
"""
|
2011-03-02 20:47:36 +08:00
|
|
|
Returns a cache key based on the request path and query. It can be used
|
|
|
|
in the request phase because it pulls the list of headers to take into
|
|
|
|
account from the global path registry and uses those to build a cache key
|
|
|
|
to check against.
|
2005-10-09 07:19:21 +08:00
|
|
|
|
|
|
|
If there is no headerlist stored, the page needs to be rebuilt, so this
|
|
|
|
function returns None.
|
|
|
|
"""
|
|
|
|
if key_prefix is None:
|
|
|
|
key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
|
2009-03-31 06:37:59 +08:00
|
|
|
cache_key = _generate_cache_header_key(key_prefix, request)
|
2010-12-22 15:52:44 +08:00
|
|
|
if cache is None:
|
|
|
|
cache = get_cache(settings.CACHE_MIDDLEWARE_ALIAS)
|
2005-10-09 07:19:21 +08:00
|
|
|
headerlist = cache.get(cache_key, None)
|
|
|
|
if headerlist is not None:
|
2010-10-29 09:31:15 +08:00
|
|
|
return _generate_cache_key(request, method, headerlist, key_prefix)
|
2005-10-09 07:19:21 +08:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2010-12-22 15:52:44 +08:00
|
|
|
def learn_cache_key(request, response, cache_timeout=None, key_prefix=None, cache=None):
|
2005-10-09 07:19:21 +08:00
|
|
|
"""
|
|
|
|
Learns what headers to take into account for some request path from the
|
|
|
|
response object. It stores those headers in a global path registry so that
|
|
|
|
later access to that path 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.
|
|
|
|
"""
|
|
|
|
if key_prefix is None:
|
|
|
|
key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
|
|
|
|
if cache_timeout is None:
|
|
|
|
cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
|
2009-03-31 06:37:59 +08:00
|
|
|
cache_key = _generate_cache_header_key(key_prefix, request)
|
2010-12-22 15:52:44 +08:00
|
|
|
if cache is None:
|
|
|
|
cache = get_cache(settings.CACHE_MIDDLEWARE_ALIAS)
|
2005-10-09 07:19:21 +08:00
|
|
|
if response.has_header('Vary'):
|
2007-10-29 06:32:25 +08:00
|
|
|
headerlist = ['HTTP_'+header.upper().replace('-', '_')
|
2007-11-19 11:12:19 +08:00
|
|
|
for header in cc_delim_re.split(response['Vary'])]
|
2005-10-09 07:19:21 +08:00
|
|
|
cache.set(cache_key, headerlist, cache_timeout)
|
2010-10-29 09:31:15 +08:00
|
|
|
return _generate_cache_key(request, request.method, headerlist, key_prefix)
|
2005-10-09 07:19:21 +08:00
|
|
|
else:
|
|
|
|
# if there is no Vary header, we still need a cache key
|
2011-03-02 20:47:36 +08:00
|
|
|
# for the request.get_full_path()
|
2005-10-09 07:19:21 +08:00
|
|
|
cache.set(cache_key, [], cache_timeout)
|
2010-10-29 09:31:15 +08:00
|
|
|
return _generate_cache_key(request, request.method, [], key_prefix)
|
2007-11-30 00:57:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _to_tuple(s):
|
|
|
|
t = s.split('=',1)
|
|
|
|
if len(t) == 2:
|
|
|
|
return t[0].lower(), t[1]
|
|
|
|
return t[0].lower(), True
|