Ticket #580: cachekey.diff

File cachekey.diff, 3.6 KB (added by hugo, 19 years ago)

cache key creation that is based on some http headers

  • django/utils/cache.py

     
     1"this module contains helper methods for the caching stuff"
     2
     3import md5
     4
     5def 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

     
    22from django.core.cache import cache
    33from django.utils.httpwrappers import HttpResponseNotModified
    44from django.utils.text import compress_string
     5from django.utils.cache import get_cache_key
    56import datetime, md5
    67
    78class CacheMiddleware:
     
    3132            request._cache_middleware_set_cache = False
    3233            return None # Don't bother checking the cache.
    3334
    34         accept_encoding = ''
    35         if settings.CACHE_MIDDLEWARE_GZIP:
    36             try:
    37                 accept_encoding = request.META['HTTP_ACCEPT_ENCODING']
    38             except KeyError:
    39                 pass
    40         accepts_gzip = 'gzip' in accept_encoding
    41         request._cache_middleware_accepts_gzip = accepts_gzip
    42 
    4335        # This uses the same cache_key as views.decorators.cache.cache_page,
    4436        # 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)
    4738        request._cache_middleware_key = cache_key
    4839
    4940        response = cache.get(cache_key, None)
  • django/views/decorators/cache.py

     
    11from django.core.cache import cache
    22from django.utils.httpwrappers import HttpResponseNotModified
    33from django.utils.text import compress_string
     4from django.utils.cache import get_cache_key
    45import datetime, md5
    56
    67def cache_page(view_func, cache_timeout, key_prefix=''):
     
    1617    unique across all Django instances on a particular server.
    1718    """
    1819    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)
    2521        response = cache.get(cache_key, None)
    2622        if response is None:
    2723            response = view_func(request, *args, **kwargs)
Back to Top