Ticket #580: cachekey.diff
File cachekey.diff, 3.6 KB (added by , 19 years ago) |
---|
-
django/utils/cache.py
1 "this module contains helper methods for the caching stuff" 2 3 import md5 4 5 def get_cache_key(request): 6 """ 7 This function returns a cache key based on a request. It takes 8 into account the URL and some HTTP headers and combines them 9 as a MD5 hexdigest to produce a key for the cache. Additionally 10 it prefixes it with some config values and constants. 11 """ 12 13 accept_encoding = '' 14 if settings.CACHE_MIDDLEWARE_GZIP: 15 try: 16 accept_encoding = request.META['HTTP_ACCEPT_ENCODING'] 17 except KeyError: 18 pass 19 accepts_gzip = 'gzip' in accept_encoding 20 request._cache_middleware_accepts_gzip = accepts_gzip 21 22 ctx = md5.new() 23 ctx.update(request.path) 24 ctx.update(str(accepts_gzip)) 25 ctx.update(request.META.get('HTTP_COOKIE', '')) 26 ctx.update(request.META.get('HTTP_ACCEPT_LANGUAGE', '')) 27 28 cache_key = 'views.decorators.cache.cache_page.%s.%s' % \ 29 (settings.CACHE_MIDDLEWARE_KEY_PREFIX, ctx.hexdigest()) 30 return cache_key 31 -
django/middleware/cache.py
2 2 from django.core.cache import cache 3 3 from django.utils.httpwrappers import HttpResponseNotModified 4 4 from django.utils.text import compress_string 5 from django.utils.cache import get_cache_key 5 6 import datetime, md5 6 7 7 8 class CacheMiddleware: … … 31 32 request._cache_middleware_set_cache = False 32 33 return None # Don't bother checking the cache. 33 34 34 accept_encoding = ''35 if settings.CACHE_MIDDLEWARE_GZIP:36 try:37 accept_encoding = request.META['HTTP_ACCEPT_ENCODING']38 except KeyError:39 pass40 accepts_gzip = 'gzip' in accept_encoding41 request._cache_middleware_accepts_gzip = accepts_gzip42 43 35 # This uses the same cache_key as views.decorators.cache.cache_page, 44 36 # so the cache can be shared. 45 cache_key = 'views.decorators.cache.cache_page.%s.%s.%s' % \ 46 (settings.CACHE_MIDDLEWARE_KEY_PREFIX, request.path, accepts_gzip) 37 cache_key = get_cache_key(request) 47 38 request._cache_middleware_key = cache_key 48 39 49 40 response = cache.get(cache_key, None) -
django/views/decorators/cache.py
1 1 from django.core.cache import cache 2 2 from django.utils.httpwrappers import HttpResponseNotModified 3 3 from django.utils.text import compress_string 4 from django.utils.cache import get_cache_key 4 5 import datetime, md5 5 6 6 7 def cache_page(view_func, cache_timeout, key_prefix=''): … … 16 17 unique across all Django instances on a particular server. 17 18 """ 18 19 def _check_cache(request, *args, **kwargs): 19 try: 20 accept_encoding = request.META['HTTP_ACCEPT_ENCODING'] 21 except KeyError: 22 accept_encoding = '' 23 accepts_gzip = 'gzip' in accept_encoding 24 cache_key = 'views.decorators.cache.cache_page.%s.%s.%s' % (key_prefix, request.path, accepts_gzip) 20 cache_key = get_cache_key(request) 25 21 response = cache.get(cache_key, None) 26 22 if response is None: 27 23 response = view_func(request, *args, **kwargs)