Django

Code

Changeset 6736

Show
Ignore:
Timestamp:
11/29/07 10:57:18 (1 year ago)
Author:
mtredinnick
Message:

Fixed #5813 -- Taught the CacheMiddleware? to respect any max-age HTTP header
when setting the expiry time. Thanks, SmileyChris?.

Files:

Legend:

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

    r4816 r6736  
    11from django.conf import settings 
    22from django.core.cache import cache 
    3 from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers 
     3from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers, get_max_age 
    44 
    55class CacheMiddleware(object): 
    66    """ 
    77    Cache middleware. If this is enabled, each Django-powered page will be 
    8     cached for CACHE_MIDDLEWARE_SECONDS seconds. Cache is based on URLs
     8    cached (based on URLs)
    99 
    1010    Only parameter-less GET or HEAD-requests with status code 200 are cached. 
     11 
     12    The number of seconds each page is stored for is set by the 
     13    "max-age" section of the response's "Cache-Control" header, falling back to 
     14    the CACHE_MIDDLEWARE_SECONDS setting if the section was not found. 
    1115 
    1216    If CACHE_MIDDLEWARE_ANONYMOUS_ONLY is set to True, only anonymous requests 
     
    7983        if not response.status_code == 200: 
    8084            return response 
    81         patch_response_headers(response, self.cache_timeout) 
    82         cache_key = learn_cache_key(request, response, self.cache_timeout, self.key_prefix) 
    83         cache.set(cache_key, response, self.cache_timeout) 
     85        # Try to get the timeout from the "max-age" section of the "Cache- 
     86        # Control" header before reverting to using the default cache_timeout 
     87        # length. 
     88        timeout = get_max_age(response) 
     89        if timeout == None: 
     90            timeout = self.cache_timeout 
     91        elif timeout == 0: 
     92            # max-age was set to 0, don't bother caching. 
     93            return response 
     94        patch_response_headers(response, timeout) 
     95        cache_key = learn_cache_key(request, response, timeout, self.key_prefix) 
     96        cache.set(cache_key, response, timeout) 
    8497        return response 
  • django/trunk/django/utils/cache.py

    r6698 r6736  
    7474    cc = ', '.join([dictvalue(el) for el in cc.items()]) 
    7575    response['Cache-Control'] = cc 
     76 
     77def get_max_age(response): 
     78    """ 
     79    Returns the max-age from the response Cache-Control header as an integer 
     80    (or ``None`` if it wasn't found or wasn't an integer. 
     81    """ 
     82    if not response.has_header('Cache-Control'): 
     83        return 
     84    cc = dict([_to_tuple(el) for el in 
     85        cc_delim_re.split(response['Cache-Control'])]) 
     86    if 'max-age' in cc: 
     87        try: 
     88            return int(cc['max-age']) 
     89        except (ValueError, TypeError): 
     90            pass 
    7691 
    7792def patch_response_headers(response, cache_timeout=None): 
     
    181196        cache.set(cache_key, [], cache_timeout) 
    182197        return _generate_cache_key(request, [], key_prefix) 
     198 
     199 
     200def _to_tuple(s): 
     201    t = s.split('=',1) 
     202    if len(t) == 2: 
     203        return t[0].lower(), t[1] 
     204    return t[0].lower(), True 
  • django/trunk/docs/cache.txt

    r6633 r6736  
    264264.. _`middleware documentation`: ../middleware/ 
    265265 
     266**New in Django development version** 
     267 
     268If a view sets its own cache expiry time (i.e. it has a ``max-age`` section in 
     269its ``Cache-Control`` header) then the page will be cached until the expiry 
     270time, rather than ``CACHE_MIDDLEWARE_SECONDS``. Using the decorators in 
     271``django.views.decorators.cache`` you can easily set a view's expiry time 
     272(using the ``cache_control`` decorator) or disable caching for a view (using 
     273the ``never_cache`` decorator). See the `using other headers`__ section for 
     274more on these decorators. 
     275 
     276__ `Controlling cache: Using other headers`_ 
     277 
    266278The per-view cache 
    267279================== 
     
    567579 
    568580If you want to use headers to disable caching altogether, 
    569 ``django.views.decorators.never_cache`` is a view decorator that adds 
     581``django.views.decorators.cache.never_cache`` is a view decorator that adds 
    570582headers to ensure the response won't be cached by browsers or other caches. Example:: 
    571583