1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

Refs #5691 -- Made cache keys independent of USE_L10N.

This mostly reverts af1893c4ff8fdbf227a43a559d90bb1c1238b01a.
This commit is contained in:
Claude Paroz 2020-05-22 19:43:03 +02:00 committed by Mariusz Felisiak
parent e37f809618
commit 258c88a913
4 changed files with 16 additions and 22 deletions

View File

@ -311,7 +311,7 @@ def has_vary_header(response, header_query):
def _i18n_cache_key_suffix(request, cache_key): def _i18n_cache_key_suffix(request, cache_key):
"""If necessary, add the current locale or time zone to the cache key.""" """If necessary, add the current locale or time zone to the cache key."""
if settings.USE_I18N or settings.USE_L10N: if settings.USE_I18N:
# first check if LocaleMiddleware or another middleware added # first check if LocaleMiddleware or another middleware added
# LANGUAGE_CODE to request, then fall back to the active language # LANGUAGE_CODE to request, then fall back to the active language
# which in turn can also fall back to settings.LANGUAGE_CODE # which in turn can also fall back to settings.LANGUAGE_CODE
@ -385,11 +385,11 @@ def learn_cache_key(request, response, cache_timeout=None, key_prefix=None, cach
if cache is None: if cache is None:
cache = caches[settings.CACHE_MIDDLEWARE_ALIAS] cache = caches[settings.CACHE_MIDDLEWARE_ALIAS]
if response.has_header('Vary'): if response.has_header('Vary'):
is_accept_language_redundant = settings.USE_I18N or settings.USE_L10N is_accept_language_redundant = settings.USE_I18N
# If i18n or l10n are used, the generated cache key will be suffixed # If i18n is used, the generated cache key will be suffixed with the
# with the current locale. Adding the raw value of Accept-Language is # current locale. Adding the raw value of Accept-Language is redundant
# redundant in that case and would result in storing the same content # in that case and would result in storing the same content under
# under multiple keys in the cache. See #18191 for details. # multiple keys in the cache. See #18191 for details.
headerlist = [] headerlist = []
for header in cc_delim_re.split(response['Vary']): for header in cc_delim_re.split(response['Vary']):
header = header.upper().replace('-', '_') header = header.upper().replace('-', '_')

View File

@ -336,6 +336,11 @@ Miscellaneous
* Support for ``argon2-cffi`` < 19.1.0 is removed. * Support for ``argon2-cffi`` < 19.1.0 is removed.
* The cache keys no longer includes the language when internationalization is
disabled (``USE_I18N = False``) and localization is enabled
(``USE_L10N = True``). After upgrading to Django 3.2 in such configurations,
the first request to any previously cached value will be a cache miss.
.. _deprecated-features-3.2: .. _deprecated-features-3.2:
Features deprecated in 3.2 Features deprecated in 3.2

View File

@ -542,8 +542,7 @@ include the name of the active :term:`language<language code>` -- see also
:ref:`how-django-discovers-language-preference`). This allows you to easily :ref:`how-django-discovers-language-preference`). This allows you to easily
cache multilingual sites without having to create the cache key yourself. cache multilingual sites without having to create the cache key yourself.
Cache keys also include the active :term:`language <language code>` when Cache keys also include the :ref:`current time zone
:setting:`USE_L10N` is set to ``True`` and the :ref:`current time zone
<default-current-time-zone>` when :setting:`USE_TZ` is set to ``True``. <default-current-time-zone>` when :setting:`USE_TZ` is set to ``True``.
__ `Controlling cache: Using other headers`_ __ `Controlling cache: Using other headers`_

18
tests/cache/tests.py vendored
View File

@ -1816,7 +1816,7 @@ class CacheI18nTest(SimpleTestCase):
def tearDown(self): def tearDown(self):
cache.clear() cache.clear()
@override_settings(USE_I18N=True, USE_L10N=False, USE_TZ=False) @override_settings(USE_I18N=True, USE_TZ=False)
def test_cache_key_i18n_translation(self): def test_cache_key_i18n_translation(self):
request = self.factory.get(self.path) request = self.factory.get(self.path)
lang = translation.get_language() lang = translation.get_language()
@ -1837,7 +1837,7 @@ class CacheI18nTest(SimpleTestCase):
self.assertEqual(key, reference_key) self.assertEqual(key, reference_key)
self.assertEqual(key2, reference_key) self.assertEqual(key2, reference_key)
@override_settings(USE_I18N=True, USE_L10N=False, USE_TZ=False) @override_settings(USE_I18N=True, USE_TZ=False)
def test_cache_key_i18n_translation_accept_language(self): def test_cache_key_i18n_translation_accept_language(self):
lang = translation.get_language() lang = translation.get_language()
self.assertEqual(lang, 'en') self.assertEqual(lang, 'en')
@ -1893,17 +1893,7 @@ class CacheI18nTest(SimpleTestCase):
key key
) )
@override_settings(USE_I18N=False, USE_L10N=True, USE_TZ=False) @override_settings(USE_I18N=False, USE_TZ=True)
def test_cache_key_i18n_formatting(self):
request = self.factory.get(self.path)
lang = translation.get_language()
response = HttpResponse()
key = learn_cache_key(request, response)
self.assertIn(lang, key, "Cache keys should include the language name when formatting is active")
key2 = get_cache_key(request)
self.assertEqual(key, key2)
@override_settings(USE_I18N=False, USE_L10N=False, USE_TZ=True)
def test_cache_key_i18n_timezone(self): def test_cache_key_i18n_timezone(self):
request = self.factory.get(self.path) request = self.factory.get(self.path)
tz = timezone.get_current_timezone_name() tz = timezone.get_current_timezone_name()
@ -1913,7 +1903,7 @@ class CacheI18nTest(SimpleTestCase):
key2 = get_cache_key(request) key2 = get_cache_key(request)
self.assertEqual(key, key2) self.assertEqual(key, key2)
@override_settings(USE_I18N=False, USE_L10N=False) @override_settings(USE_I18N=False)
def test_cache_key_no_i18n(self): def test_cache_key_no_i18n(self):
request = self.factory.get(self.path) request = self.factory.get(self.path)
lang = translation.get_language() lang = translation.get_language()