mirror of https://github.com/django/django.git
Removed settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY per deprecation timeline.
refs #15201.
This commit is contained in:
parent
8b81dee60c
commit
f567d04b24
|
@ -43,12 +43,9 @@ More details about how the caching works:
|
|||
|
||||
"""
|
||||
|
||||
import warnings
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import caches, DEFAULT_CACHE_ALIAS
|
||||
from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers, get_max_age
|
||||
from django.utils.deprecation import RemovedInDjango18Warning
|
||||
|
||||
|
||||
class UpdateCacheMiddleware(object):
|
||||
|
@ -63,7 +60,6 @@ class UpdateCacheMiddleware(object):
|
|||
def __init__(self):
|
||||
self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
|
||||
self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
|
||||
self.cache_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
|
||||
self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS
|
||||
self.cache = caches[self.cache_alias]
|
||||
|
||||
|
@ -74,17 +70,7 @@ class UpdateCacheMiddleware(object):
|
|||
return False
|
||||
|
||||
def _should_update_cache(self, request, response):
|
||||
if not hasattr(request, '_cache_update_cache') or not request._cache_update_cache:
|
||||
return False
|
||||
# If the session has not been accessed otherwise, we don't want to
|
||||
# cause it to be accessed here. If it hasn't been accessed, then the
|
||||
# user's logged-in status has not affected the response anyway.
|
||||
if self.cache_anonymous_only and self._session_accessed(request):
|
||||
assert hasattr(request, 'user'), "The Django cache middleware with CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True requires authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.auth.middleware.AuthenticationMiddleware' before the CacheMiddleware."
|
||||
if request.user.is_authenticated():
|
||||
# Don't cache user-variable requests from authenticated users.
|
||||
return False
|
||||
return True
|
||||
return hasattr(request, '_cache_update_cache') and request._cache_update_cache
|
||||
|
||||
def process_response(self, request, response):
|
||||
"""Sets the cache, if needed."""
|
||||
|
@ -163,7 +149,7 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):
|
|||
Also used as the hook point for the cache decorator, which is generated
|
||||
using the decorator-from-middleware utility.
|
||||
"""
|
||||
def __init__(self, cache_timeout=None, cache_anonymous_only=None, **kwargs):
|
||||
def __init__(self, cache_timeout=None, **kwargs):
|
||||
# We need to differentiate between "provided, but using default value",
|
||||
# and "not provided". If the value is provided using a default, then
|
||||
# we fall back to system defaults. If it is not provided at all,
|
||||
|
@ -188,13 +174,4 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):
|
|||
if cache_timeout is None:
|
||||
cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
|
||||
self.cache_timeout = cache_timeout
|
||||
|
||||
if cache_anonymous_only is None:
|
||||
cache_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
|
||||
self.cache_anonymous_only = cache_anonymous_only
|
||||
|
||||
if self.cache_anonymous_only:
|
||||
msg = "CACHE_MIDDLEWARE_ANONYMOUS_ONLY has been deprecated and will be removed in Django 1.8."
|
||||
warnings.warn(msg, RemovedInDjango18Warning, stacklevel=1)
|
||||
|
||||
self.cache = caches[self.cache_alias]
|
||||
|
|
|
@ -279,26 +279,6 @@ Default: ``default``
|
|||
|
||||
The cache connection to use for the cache middleware.
|
||||
|
||||
.. setting:: CACHE_MIDDLEWARE_ANONYMOUS_ONLY
|
||||
|
||||
CACHE_MIDDLEWARE_ANONYMOUS_ONLY
|
||||
-------------------------------
|
||||
|
||||
Default: ``False``
|
||||
|
||||
.. deprecated:: 1.6
|
||||
|
||||
This setting was largely ineffective because of using cookies for sessions
|
||||
and CSRF. See the :doc:`Django 1.6 release notes</releases/1.6>` for more
|
||||
information.
|
||||
|
||||
If the value of this setting is ``True``, only anonymous requests (i.e., not
|
||||
those made by a logged-in user) will be cached. Otherwise, the middleware
|
||||
caches every page that doesn't have GET or POST parameters.
|
||||
|
||||
If you set the value of this setting to ``True``, you should make sure you've
|
||||
activated ``AuthenticationMiddleware``.
|
||||
|
||||
.. setting:: CACHE_MIDDLEWARE_KEY_PREFIX
|
||||
|
||||
CACHE_MIDDLEWARE_KEY_PREFIX
|
||||
|
@ -2885,7 +2865,6 @@ Cache
|
|||
-----
|
||||
* :setting:`CACHES`
|
||||
* :setting:`CACHE_MIDDLEWARE_ALIAS`
|
||||
* :setting:`CACHE_MIDDLEWARE_ANONYMOUS_ONLY`
|
||||
* :setting:`CACHE_MIDDLEWARE_KEY_PREFIX`
|
||||
* :setting:`CACHE_MIDDLEWARE_SECONDS`
|
||||
|
||||
|
|
|
@ -26,8 +26,7 @@ from django.middleware.cache import (FetchFromCacheMiddleware,
|
|||
from django.template import Template
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase, TransactionTestCase, RequestFactory, override_settings
|
||||
from django.test.utils import (IgnoreDeprecationWarningsMixin,
|
||||
IgnorePendingDeprecationWarningsMixin)
|
||||
from django.test.utils import IgnorePendingDeprecationWarningsMixin
|
||||
from django.utils import six
|
||||
from django.utils import timezone
|
||||
from django.utils import translation
|
||||
|
@ -1743,7 +1742,6 @@ def hello_world_view(request, value):
|
|||
CACHE_MIDDLEWARE_ALIAS='other',
|
||||
CACHE_MIDDLEWARE_KEY_PREFIX='middlewareprefix',
|
||||
CACHE_MIDDLEWARE_SECONDS=30,
|
||||
CACHE_MIDDLEWARE_ANONYMOUS_ONLY=False,
|
||||
CACHES={
|
||||
'default': {
|
||||
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
||||
|
@ -1755,7 +1753,7 @@ def hello_world_view(request, value):
|
|||
},
|
||||
},
|
||||
)
|
||||
class CacheMiddlewareTest(IgnoreDeprecationWarningsMixin, TestCase):
|
||||
class CacheMiddlewareTest(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(CacheMiddlewareTest, self).setUp()
|
||||
|
@ -1781,7 +1779,6 @@ class CacheMiddlewareTest(IgnoreDeprecationWarningsMixin, TestCase):
|
|||
self.assertEqual(middleware.cache_timeout, 30)
|
||||
self.assertEqual(middleware.key_prefix, 'middlewareprefix')
|
||||
self.assertEqual(middleware.cache_alias, 'other')
|
||||
self.assertEqual(middleware.cache_anonymous_only, False)
|
||||
|
||||
# If arguments are being passed in construction, it's being used as a decorator.
|
||||
# First, test with "defaults":
|
||||
|
@ -1790,15 +1787,13 @@ class CacheMiddlewareTest(IgnoreDeprecationWarningsMixin, TestCase):
|
|||
self.assertEqual(as_view_decorator.cache_timeout, 30) # Timeout value for 'default' cache, i.e. 30
|
||||
self.assertEqual(as_view_decorator.key_prefix, '')
|
||||
self.assertEqual(as_view_decorator.cache_alias, 'default') # Value of DEFAULT_CACHE_ALIAS from django.core.cache
|
||||
self.assertEqual(as_view_decorator.cache_anonymous_only, False)
|
||||
|
||||
# Next, test with custom values:
|
||||
as_view_decorator_with_custom = CacheMiddleware(cache_anonymous_only=True, cache_timeout=60, cache_alias='other', key_prefix='foo')
|
||||
as_view_decorator_with_custom = CacheMiddleware(cache_timeout=60, cache_alias='other', key_prefix='foo')
|
||||
|
||||
self.assertEqual(as_view_decorator_with_custom.cache_timeout, 60)
|
||||
self.assertEqual(as_view_decorator_with_custom.key_prefix, 'foo')
|
||||
self.assertEqual(as_view_decorator_with_custom.cache_alias, 'other')
|
||||
self.assertEqual(as_view_decorator_with_custom.cache_anonymous_only, True)
|
||||
|
||||
def test_middleware(self):
|
||||
middleware = CacheMiddleware()
|
||||
|
@ -1830,57 +1825,6 @@ class CacheMiddlewareTest(IgnoreDeprecationWarningsMixin, TestCase):
|
|||
self.assertNotEqual(result, None)
|
||||
self.assertEqual(result.content, b'Hello World 1')
|
||||
|
||||
@override_settings(CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True)
|
||||
def test_cache_middleware_anonymous_only_wont_cause_session_access(self):
|
||||
""" The cache middleware shouldn't cause a session access due to
|
||||
CACHE_MIDDLEWARE_ANONYMOUS_ONLY if nothing else has accessed the
|
||||
session. Refs 13283 """
|
||||
|
||||
from django.contrib.sessions.middleware import SessionMiddleware
|
||||
from django.contrib.auth.middleware import AuthenticationMiddleware
|
||||
|
||||
middleware = CacheMiddleware()
|
||||
session_middleware = SessionMiddleware()
|
||||
auth_middleware = AuthenticationMiddleware()
|
||||
|
||||
request = self.factory.get('/view_anon/')
|
||||
|
||||
# Put the request through the request middleware
|
||||
session_middleware.process_request(request)
|
||||
auth_middleware.process_request(request)
|
||||
result = middleware.process_request(request)
|
||||
self.assertEqual(result, None)
|
||||
|
||||
response = hello_world_view(request, '1')
|
||||
|
||||
# Now put the response through the response middleware
|
||||
session_middleware.process_response(request, response)
|
||||
response = middleware.process_response(request, response)
|
||||
|
||||
self.assertEqual(request.session.accessed, False)
|
||||
|
||||
@override_settings(CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True)
|
||||
def test_cache_middleware_anonymous_only_with_cache_page(self):
|
||||
"""CACHE_MIDDLEWARE_ANONYMOUS_ONLY should still be effective when used
|
||||
with the cache_page decorator: the response to a request from an
|
||||
authenticated user should not be cached."""
|
||||
|
||||
request = self.factory.get('/view_anon/')
|
||||
|
||||
class MockAuthenticatedUser(object):
|
||||
def is_authenticated(self):
|
||||
return True
|
||||
|
||||
class MockAccessedSession(object):
|
||||
accessed = True
|
||||
|
||||
request.user = MockAuthenticatedUser()
|
||||
request.session = MockAccessedSession()
|
||||
|
||||
response = cache_page(60)(hello_world_view)(request, '1')
|
||||
|
||||
self.assertFalse("Cache-Control" in response)
|
||||
|
||||
def test_view_decorator(self):
|
||||
# decorate the same view with different cache decorators
|
||||
default_view = cache_page(3)(hello_world_view)
|
||||
|
|
Loading…
Reference in New Issue