Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added a CACHE_MIDDLEWARE_ANONYMOUS_ONLY setting which makes the cache…

… ignore pages served to authenticated users. Fixes #1509 (thanks, Matt).

Also added a FAQ entry about using this setting to avoid caching of the admin interface. 


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3395 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2a8a32c44902ab13d68c36e010331d9b6c969fbf 1 parent 9b6d1ef
Jacob Kaplan-Moss authored July 20, 2006
15  django/middleware/cache.py
@@ -10,6 +10,11 @@ class CacheMiddleware(object):
10 10
 
11 11
     Only parameter-less GET or HEAD-requests with status code 200 are cached.
12 12
 
  13
+    If CACHE_MIDDLEWARE_ANONYMOUS_ONLY is set to True, only anonymous requests
  14
+    (i.e. those node made by a logged in user) will be cached. This is a
  15
+    simple and effective way of avoiding the caching of the Django admin (and
  16
+    any other user-specific content).
  17
+
13 18
     This middleware expects that a HEAD request is answered with a response
14 19
     exactly like the corresponding GET request.
15 20
 
@@ -23,13 +28,17 @@ class CacheMiddleware(object):
23 28
     This middleware also sets ETag, Last-Modified, Expires and Cache-Control
24 29
     headers on the response object.
25 30
     """
26  
-    def __init__(self, cache_timeout=None, key_prefix=None):
  31
+    def __init__(self, cache_timeout=None, key_prefix=None, cache_anonymous_only=None):
27 32
         self.cache_timeout = cache_timeout
28 33
         if cache_timeout is None:
29 34
             self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
30 35
         self.key_prefix = key_prefix
31 36
         if key_prefix is None:
32 37
             self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
  38
+        if cache_anonymous is None:
  39
+            self.cache_anonymous_only = settings.get('CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
  40
+        else:
  41
+            self.cache_anonymous_only = cache_anonymous_only
33 42
 
34 43
     def process_request(self, request):
35 44
         "Checks whether the page is already cached and returns the cached version if available."
@@ -37,6 +46,10 @@ def process_request(self, request):
37 46
             request._cache_update_cache = False
38 47
             return None # Don't bother checking the cache.
39 48
 
  49
+        if self.cache_anonymous_only and request.user.is_authenticated():
  50
+            request._cache_update_cache = False
  51
+            return None # Don't cache requests from authenticated users.
  52
+
40 53
         cache_key = get_cache_key(request, self.key_prefix)
41 54
         if cache_key is None:
42 55
             request._cache_update_cache = True
7  docs/cache.txt
@@ -230,7 +230,12 @@ Then, add the following required settings to your Django settings file:
230 230
   collisions. Use an empty string if you don't care.
231 231
 
232 232
 The cache middleware caches every page that doesn't have GET or POST
233  
-parameters. Additionally, ``CacheMiddleware`` automatically sets a few headers
  233
+parameters. Optionally, If the ``CACHE_MIDDLEWARE_ANONYMOUS_ONLY`` setting is
  234
+``True``, only anonymous requests (i.e. those node made by a logged in user)
  235
+will be cached. This is a simple and effective way of disabling caching on any
  236
+user-specific content ( include Django's admin interface).
  237
+
  238
+Additionally, ``CacheMiddleware`` automatically sets a few headers
234 239
 in each ``HttpResponse``:
235 240
 
236 241
 * Sets the ``Last-Modified`` header to the current date/time when a fresh
8  docs/faq.txt
@@ -535,6 +535,14 @@ If you're sure your username and password are correct, make sure your user
535 535
 account has ``is_active`` and ``is_staff`` set to True. The admin site only
536 536
 allows access to users with those two fields both set to True.
537 537
 
  538
+How can I prevent the cache middleware from caching the admin site?
  539
+-------------------------------------------------------------------
  540
+
  541
+Set the ``CACHE_MIDDLEWARE_ANONYMOUS_ONLY`` setting to ``True``. See the
  542
+`cache documentation`_ for more information.
  543
+
  544
+.. _cache documentation: ../cache/#the-per-site-cache
  545
+
538 546
 How do I automatically set a field's value to the user who last edited the object in the admin?
539 547
 -----------------------------------------------------------------------------------------------
540 548
 

0 notes on commit 2a8a32c

Please sign in to comment.
Something went wrong with that request. Please try again.