Django

Code

Changeset 3395

Show
Ignore:
Timestamp:
07/20/06 10:37:12 (2 years ago)
Author:
jacob
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/middleware/cache.py

    r3171 r3395  
    1010 
    1111    Only parameter-less GET or HEAD-requests with status code 200 are cached. 
     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). 
    1217 
    1318    This middleware expects that a HEAD request is answered with a response 
     
    2429    headers on the response object. 
    2530    """ 
    26     def __init__(self, cache_timeout=None, key_prefix=None): 
     31    def __init__(self, cache_timeout=None, key_prefix=None, cache_anonymous_only=None): 
    2732        self.cache_timeout = cache_timeout 
    2833        if cache_timeout is None: 
     
    3136        if key_prefix is None: 
    3237            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 
    3342 
    3443    def process_request(self, request): 
     
    3746            request._cache_update_cache = False 
    3847            return None # Don't bother checking the cache. 
     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. 
    3952 
    4053        cache_key = get_cache_key(request, self.key_prefix) 
  • django/trunk/docs/cache.txt

    r2980 r3395  
    231231 
    232232The cache middleware caches every page that doesn't have GET or POST 
    233 parameters. Additionally, ``CacheMiddleware`` automatically sets a few headers 
     233parameters. Optionally, If the ``CACHE_MIDDLEWARE_ANONYMOUS_ONLY`` setting is 
     234``True``, only anonymous requests (i.e. those node made by a logged in user) 
     235will be cached. This is a simple and effective way of disabling caching on any 
     236user-specific content ( include Django's admin interface). 
     237 
     238Additionally, ``CacheMiddleware`` automatically sets a few headers 
    234239in each ``HttpResponse``: 
    235240 
  • django/trunk/docs/faq.txt

    r3236 r3395  
    536536allows access to users with those two fields both set to True. 
    537537 
     538How can I prevent the cache middleware from caching the admin site? 
     539------------------------------------------------------------------- 
     540 
     541Set 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 
    538546How do I automatically set a field's value to the user who last edited the object in the admin? 
    539547-----------------------------------------------------------------------------------------------