Ticket #5813: cache_middleware_respect_maxage.diff
File cache_middleware_respect_maxage.diff, 4.5 KB (added by , 17 years ago) |
---|
-
docs/cache.txt
263 263 264 264 .. _`middleware documentation`: ../middleware/ 265 265 266 **New in Django development version** 267 268 If the view sets its own cache expiry time (i.e. has a ``max-age`` section in 269 it's ``Cache-Control`` header) then the page will be cached for the length of 270 time given in there, rather than ``CACHE_MIDDLEWARE_SECONDS``. Using the 271 decorators in ``django.views.decorators.cache`` you can easily set a view's 272 expiry time (by using the ``cache_control`` decorator) or disable caching for a 273 view (by using the ``never_cache`` decorator). See the `using other headers`__ 274 section for more on these decorators. 275 276 __ `Controlling cache: Using other headers`_ 277 266 278 The per-view cache 267 279 ================== 268 280 … … 564 576 precedence, and the header values will be merged correctly.) 565 577 566 578 If you want to use headers to disable caching altogether, 567 ``django.views.decorators. never_cache`` is a view decorator that adds579 ``django.views.decorators.cache.never_cache`` is a view decorator that adds 568 580 headers to ensure the response won't be cached by browsers or other caches. Example:: 569 581 570 582 from django.views.decorators.cache import never_cache -
django/utils/cache.py
69 69 cc = ', '.join([dictvalue(el) for el in cc.items()]) 70 70 response['Cache-Control'] = cc 71 71 72 def get_max_age(response): 73 """ 74 Returns the max-age from the response Cache-Control header as an integer (or 75 ``None`` if it wasn't found or wasn't an integer. 76 """ 77 def dictitem(s): 78 t = s.split('=',1) 79 if len(t) > 1: 80 return (t[0].lower(), t[1]) 81 else: 82 return (t[0].lower(), True) 83 if response.has_header('Cache-Control'): 84 cc = cc_delim_re.split(response['Cache-Control']) 85 cc = dict([dictitem(el) for el in cc]) 86 if 'max-age' in cc: 87 try: 88 return int(cc['max-age']) 89 except ValueError, TypeError: 90 pass 91 return None 92 72 93 vary_delim_re = re.compile(r',\s*') 73 94 74 95 def patch_response_headers(response, cache_timeout=None): -
django/middleware/cache.py
1 1 from django.conf import settings 2 2 from django.core.cache import cache 3 from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers 3 from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers, get_max_age 4 4 5 5 class CacheMiddleware(object): 6 6 """ 7 7 Cache middleware. If this is enabled, each Django-powered page will be 8 cached for CACHE_MIDDLEWARE_SECONDS seconds. Cache is based on URLs.8 cached (based on URLs). 9 9 10 10 Only parameter-less GET or HEAD-requests with status code 200 are cached. 11 11 12 The number of seconds each page is stored for is set by the 13 "max-age" section of the response's "Cache-Control" header, falling back to 14 the CACHE_MIDDLEWARE_SECONDS setting if the section was not found. 15 12 16 If CACHE_MIDDLEWARE_ANONYMOUS_ONLY is set to True, only anonymous requests 13 17 (i.e., those not made by a logged-in user) will be cached. This is a 14 18 simple and effective way of avoiding the caching of the Django admin (and … … 78 82 return response 79 83 if not response.status_code == 200: 80 84 return response 81 patch_response_headers(response, self.cache_timeout) 82 cache_key = learn_cache_key(request, response, self.cache_timeout, self.key_prefix) 83 cache.set(cache_key, response, self.cache_timeout) 85 # Try to get the timeout from the "max-age" section of the "Cache- 86 # Control" header before reverting to using the default cache_timeout 87 # length. 88 timeout = get_max_age(response) 89 if timeout == None: 90 timeout = self.cache_timeout 91 elif timeout == 0: 92 # max-age was set to 0, don't bother caching. 93 return response 94 patch_response_headers(response, timeout) 95 cache_key = learn_cache_key(request, response, timeout, self.key_prefix) 96 cache.set(cache_key, response, timeout) 84 97 return response