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

This mostly reverts af1893c4ff.
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):
"""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
# LANGUAGE_CODE to request, then fall back to the active language
# 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:
cache = caches[settings.CACHE_MIDDLEWARE_ALIAS]
if response.has_header('Vary'):
is_accept_language_redundant = settings.USE_I18N or settings.USE_L10N
# If i18n or l10n are used, the generated cache key will be suffixed
# with the current locale. Adding the raw value of Accept-Language is
# redundant in that case and would result in storing the same content
# under multiple keys in the cache. See #18191 for details.
is_accept_language_redundant = settings.USE_I18N
# If i18n is used, the generated cache key will be suffixed with the
# current locale. Adding the raw value of Accept-Language is redundant
# in that case and would result in storing the same content under
# multiple keys in the cache. See #18191 for details.
headerlist = []
for header in cc_delim_re.split(response['Vary']):
header = header.upper().replace('-', '_')

View File

@ -336,6 +336,11 @@ Miscellaneous
* 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:
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
cache multilingual sites without having to create the cache key yourself.
Cache keys also include the active :term:`language <language code>` when
:setting:`USE_L10N` is set to ``True`` and the :ref:`current time zone
Cache keys also include the :ref:`current time zone
<default-current-time-zone>` when :setting:`USE_TZ` is set to ``True``.
__ `Controlling cache: Using other headers`_

18
tests/cache/tests.py vendored
View File

@ -1816,7 +1816,7 @@ class CacheI18nTest(SimpleTestCase):
def tearDown(self):
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):
request = self.factory.get(self.path)
lang = translation.get_language()
@ -1837,7 +1837,7 @@ class CacheI18nTest(SimpleTestCase):
self.assertEqual(key, 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):
lang = translation.get_language()
self.assertEqual(lang, 'en')
@ -1893,17 +1893,7 @@ class CacheI18nTest(SimpleTestCase):
key
)
@override_settings(USE_I18N=False, USE_L10N=True, USE_TZ=False)
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)
@override_settings(USE_I18N=False, USE_TZ=True)
def test_cache_key_i18n_timezone(self):
request = self.factory.get(self.path)
tz = timezone.get_current_timezone_name()
@ -1913,7 +1903,7 @@ class CacheI18nTest(SimpleTestCase):
key2 = get_cache_key(request)
self.assertEqual(key, key2)
@override_settings(USE_I18N=False, USE_L10N=False)
@override_settings(USE_I18N=False)
def test_cache_key_no_i18n(self):
request = self.factory.get(self.path)
lang = translation.get_language()