Ticket #5691: cache.patch

File cache.patch, 2.5 KB (added by aaloy, 7 years ago)

cache pacth to use lang in the cache key

  • django/utils/cache.py

     
    2929from django.utils.encoding import smart_str, iri_to_uri
    3030from django.utils.http import http_date
    3131from django.utils.hashcompat import md5_constructor
     32from django.utils import translation
    3233
    3334cc_delim_re = re.compile(r'\s*,\s*')
    3435
     
    136137                          if newheader.lower() not in existing_headers]
    137138    response['Vary'] = ', '.join(vary_headers + additional_headers)
    138139
    139 def _generate_cache_key(request, headerlist, key_prefix):
     140def _generate_cache_key(request, headerlist, key_prefix):   
    140141    """Returns a cache key from the headers given in the header list."""
    141142    ctx = md5_constructor()
    142143    for header in headerlist:
    143144        value = request.META.get(header, None)
    144145        if value is not None:
    145146            ctx.update(value)
    146     return 'views.decorators.cache.cache_page.%s.%s.%s' % (
     147    cache_key = 'views.decorators.cache.cache_page.%s.%s.%s' % (
    147148               key_prefix, iri_to_uri(request.path), ctx.hexdigest())
    148149
     150    if settings.USE_I18N:
     151        cache_key += ".%s" % translation.get_language()
     152    return cache_key
     153
     154
    149155def get_cache_key(request, key_prefix=None):
    150156    """
    151157    Returns a cache key based on the request path. It can be used in the
     
    155161
    156162    If there is no headerlist stored, the page needs to be rebuilt, so this
    157163    function returns None.
    158     """
     164    """   
    159165    if key_prefix is None:
    160166        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    161167    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
    162168                    key_prefix, iri_to_uri(request.path))
     169    if settings.USE_I18N:
     170        cache_key += ".%s" % translation.get_language()
     171 
    163172    headerlist = cache.get(cache_key, None)
    164173    if headerlist is not None:
    165174        return _generate_cache_key(request, headerlist, key_prefix)
     
    183192        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    184193    if cache_timeout is None:
    185194        cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
     195
    186196    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
    187197                    key_prefix, iri_to_uri(request.path))
     198    if settings.USE_I18N:
     199        cache_key += ".%s" % translation.get_language()
    188200    if response.has_header('Vary'):
    189201        headerlist = ['HTTP_'+header.upper().replace('-', '_')
    190202                      for header in cc_delim_re.split(response['Vary'])]
Back to Top