Ticket #612: cache-control.diff
File cache-control.diff, 5.2 KB (added by , 19 years ago) |
---|
-
django/utils/cache.py
21 21 from django.conf import settings 22 22 from django.core.cache import cache 23 23 24 cc_delim_re = re.compile(r'\s*,\s*') 25 def patch_cache_control(response, **kwargs): 26 """ 27 This function patches the Cache-Control header by adding all 28 keyword arguments to it. The transformation is as follows: 29 30 - all keyword parameter names are turned to lowercase and 31 all _ will be translated to - 32 - if the value of a parameter is True (exatly True, not just a 33 true value), only the parameter name is added to the header 34 - all other parameters are added with their value, after applying 35 str to it. 36 """ 37 38 def dictitem(s): 39 t = s.split('=',1) 40 if len(t) > 1: 41 return (t[0].lower().replace('-', '_'), t[1]) 42 else: 43 return (t[0].lower().replace('-', '_'), True) 44 45 def dictvalue(t): 46 if t[1] == True: 47 return t[0] 48 else: 49 return t[0] + '=' + str(t[1]) 50 51 if response.has_header('Cache-Control'): 52 print response['Cache-Control'] 53 cc = cc_delim_re.split(response['Cache-Control']) 54 print cc 55 cc = dict([dictitem(el) for el in cc]) 56 else: 57 cc = {} 58 for (k,v) in kwargs.items(): 59 cc[k.replace('_', '-')] = v 60 cc = ', '.join([dictvalue(el) for el in cc.items()]) 61 response['Cache-Control'] = cc 62 24 63 vary_delim_re = re.compile(r',\s*') 25 64 26 65 def patch_response_headers(response, cache_timeout=None): … … 43 82 response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT') 44 83 if not response.has_header('Expires'): 45 84 response['Expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S GMT') 46 if not response.has_header('Cache-Control'): 47 response['Cache-Control'] = 'max-age=%d' % cache_timeout 85 patch_cache_control(response, max_age=cache_timeout) 48 86 49 87 def patch_vary_headers(response, newheaders): 50 88 """ -
django/views/decorators/cache.py
10 10 Additionally, all headers from the response's Vary header will be taken into 11 11 account on caching -- just like the middleware does. 12 12 """ 13 import re 13 14 14 15 from django.utils.decorators import decorator_from_middleware 16 from django.utils.cache import patch_cache_control 15 17 from django.middleware.cache import CacheMiddleware 16 18 17 19 cache_page = decorator_from_middleware(CacheMiddleware) 20 21 def cache_control(**kwargs): 22 23 def _cache_controller(viewfunc): 24 25 def _cache_controlled(request, *args, **kw): 26 response = viewfunc(request, *args, **kw) 27 patch_cache_control(response, **kwargs) 28 return response 29 30 return _cache_controlled 31 32 return _cache_controller 33 -
docs/cache.txt
270 270 271 271 .. _`HTTP Vary headers`: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44 272 272 273 Controlling cache: Using Vary headers 274 ===================================== 275 276 Another problem with caching is the privacy of data, and the question where data can 277 be stored in a cascade of caches. A user usually faces two kinds of caches: his own 278 browser cache (a private cache) and his providers cache (a public cache). A public cache 279 is used by multiple users and controlled by someone else. This poses problems with private 280 (in the sense of sensitive) data - you don't want your social security number or your 281 banking account numbers stored in some public cache. So web applications need a way 282 to tell the caches what data is private and what is public. 283 284 Other aspects are the definition how long a page should be cached at max, or wether the 285 cache should allways check for newer versions and only deliver the cache content when 286 there were no changes (some caches might deliver cached content even if the server page 287 changed - just because the cache copy isn't yet expired). 288 289 So there are a multitude of options you can control for your pages. This is where the 290 Cache-Control header (more infos in `HTTP Cache-Control headers`_) comes in. The usage 291 is quite simple:: 292 293 @cache_control(private=True, must_revalidate=True, max_age=3600) 294 def my_view(request): 295 ... 296 297 This would define the view as private, to be revalidated on every access and cache 298 copies will only be stored for 3600 seconds at max. 299 300 The caching middleware already set's this header up with a max-age of the CACHE_MIDDLEWARE_SETTINGS 301 setting. And the cache_page decorator does the same. The cache_control decorator correctly merges 302 different values into one big header, though. But you should take into account that middlewares 303 might overwrite some of your headers or set their own defaults if you don't give that header yourself. 304 305 .. _`HTTP Cache-Control headers`: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 306 273 307 Other optimizations 274 308 =================== 275 309