Code

Ticket #1509: anonymous_caching.patch

File anonymous_caching.patch, 2.2 KB (added by matt, 8 years ago)

patch for caching anonymous-only

  • Users/mcroydon/django/django_src.trunk/django/middleware/cache.py

     
    99    cached for CACHE_MIDDLEWARE_SECONDS seconds. Cache is based on URLs. 
    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    (those not made by a logged in user) are cached. 
    1215 
    1316    This middleware expects that a HEAD request is answered with a response 
    1417    exactly like the corresponding GET request. 
     
    2326    This middleware also sets ETag, Last-Modified, Expires and Cache-Control 
    2427    headers on the response object. 
    2528    """ 
    26     def __init__(self, cache_timeout=None, key_prefix=None): 
     29    def __init__(self, cache_timeout=None, key_prefix=None, cache_anonymous=None): 
    2730        self.cache_timeout = cache_timeout 
    2831        if cache_timeout is None: 
    2932            self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS 
    3033        self.key_prefix = key_prefix 
    3134        if key_prefix is None: 
    3235            self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX 
     36        self.cache_anonymous = cache_anonymous 
     37        if cache_anonymous is None and hasattr(settings, "CACHE_MIDDLEWARE_ANONYMOUS_ONLY"): 
     38            self.cache_anonymous = settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY 
    3339 
    3440    def process_request(self, request): 
    35         "Checks whether the page is already cached and returns the cached version if available." 
     41        "Checks whether the page is already cached and returns the cached version if available."              
    3642        if not request.META['REQUEST_METHOD'] in ('GET', 'HEAD') or request.GET: 
    3743            request._cache_update_cache = False 
    3844            return None # Don't bother checking the cache. 
     45             
     46        if self.cache_anonymous and not request.user.is_anonymous(): 
     47            request._cache_update_cache = False 
     48            return None # Don't cache logged in users 
    3949 
    4050        cache_key = get_cache_key(request, self.key_prefix) 
    4151        if cache_key is None: