summaryrefslogtreecommitdiff
path: root/django/utils/cache.py
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2010-03-01 10:19:01 +0000
committerJannis Leidel <jannis@leidel.info>2010-03-01 10:19:01 +0000
commit284e7e3cbdc1b0efb46f35485194d6a5f9b201dc (patch)
tree438d2b7a522a661adef70c81d9c25582cb7456ac /django/utils/cache.py
parent68dd63b984b824407f59280515fc94d853ec3b7c (diff)
Refined changes made in r12546 to also respect the request.LANGUAGE_CODE in case the LocaleMiddleware is used to discover the language preference.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12624 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/cache.py')
-rw-r--r--django/utils/cache.py19
1 files changed, 12 insertions, 7 deletions
diff --git a/django/utils/cache.py b/django/utils/cache.py
index 9828556057..6cfd893668 100644
--- a/django/utils/cache.py
+++ b/django/utils/cache.py
@@ -25,7 +25,7 @@ from django.core.cache import cache
from django.utils.encoding import smart_str, iri_to_uri
from django.utils.http import http_date
from django.utils.hashcompat import md5_constructor
-from django.utils import translation
+from django.utils.translation import get_language
from django.http import HttpRequest
cc_delim_re = re.compile(r'\s*,\s*')
@@ -134,6 +134,15 @@ def patch_vary_headers(response, newheaders):
if newheader.lower() not in existing_headers]
response['Vary'] = ', '.join(vary_headers + additional_headers)
+def _i18n_cache_key_suffix(request, cache_key):
+ """If enabled, returns the cache key ending with a locale."""
+ if settings.USE_I18N:
+ # first check if LocaleMiddleware or another middleware added
+ # LANGUAGE_CODE to request, then fall back to the active language
+ # which in turn can also fall back to settings.LANGUAGE_CODE
+ cache_key += '.%s' % getattr(request, 'LANGUAGE_CODE', get_language())
+ return cache_key
+
def _generate_cache_key(request, headerlist, key_prefix):
"""Returns a cache key from the headers given in the header list."""
ctx = md5_constructor()
@@ -144,18 +153,14 @@ def _generate_cache_key(request, headerlist, key_prefix):
path = md5_constructor(iri_to_uri(request.path))
cache_key = 'views.decorators.cache.cache_page.%s.%s.%s' % (
key_prefix, path.hexdigest(), ctx.hexdigest())
- if settings.USE_I18N:
- cache_key += '.%s' % translation.get_language()
- return cache_key
+ return _i18n_cache_key_suffix(request, cache_key)
def _generate_cache_header_key(key_prefix, request):
"""Returns a cache key for the header cache."""
path = md5_constructor(iri_to_uri(request.path))
cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
key_prefix, path.hexdigest())
- if settings.USE_I18N:
- cache_key += ".%s" % translation.get_language()
- return cache_key
+ return _i18n_cache_key_suffix(request, cache_key)
def get_cache_key(request, key_prefix=None):
"""