diff --git a/django/utils/cache.py b/django/utils/cache.py index df9c4c755a..72f017f38a 100644 --- a/django/utils/cache.py +++ b/django/utils/cache.py @@ -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('-', '_') diff --git a/docs/releases/3.2.txt b/docs/releases/3.2.txt index a9e9ff662d..4308d142e8 100644 --- a/docs/releases/3.2.txt +++ b/docs/releases/3.2.txt @@ -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 diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index 330b40dad7..cc9b22ae65 100644 --- a/docs/topics/cache.txt +++ b/docs/topics/cache.txt @@ -542,8 +542,7 @@ include the name of the active :term:`language` -- 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 ` when -:setting:`USE_L10N` is set to ``True`` and the :ref:`current time zone +Cache keys also include the :ref:`current time zone ` when :setting:`USE_TZ` is set to ``True``. __ `Controlling cache: Using other headers`_ diff --git a/tests/cache/tests.py b/tests/cache/tests.py index dc4a221060..60a4ce51bb 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -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()