Ticket #5815: add_cache_clear_feature.diff

File add_cache_clear_feature.diff, 3.7 KB (added by k0001, 8 years ago)

adds per-view clear cache support

  • django/utils/cache.py

     
    158158    cache as the pages themselves. If the cache ages some data out of the
    159159    cache, this just means that we have to build the response once to get at
    160160    the Vary header and so at the list of headers to use for the cache key.
     161   
     162    Also, another key (hashes) is added to the cache, to keep track of every
     163    page hash that matches the same request path.
    161164    """
    162165    if key_prefix is None:
    163166        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    164167    if cache_timeout is None:
    165168        cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
    166     cache_key = 'views.decorators.cache.cache_header.%s.%s' % (key_prefix, iri_to_uri(request.path))
     169    header_key = 'views.decorators.cache.cache_header.%s.%s' % (key_prefix, iri_to_uri(request.path))
    167170    if response.has_header('Vary'):
    168171        headerlist = ['HTTP_'+header.upper().replace('-', '_') for header in vary_delim_re.split(response['Vary'])]
    169         cache.set(cache_key, headerlist, cache_timeout)
    170         return _generate_cache_key(request, headerlist, key_prefix)
    171172    else:
    172         # if there is no Vary header, we still need a cache key
    173         # for the request.path
    174         cache.set(cache_key, [], cache_timeout)
    175         return _generate_cache_key(request, [], key_prefix)
     173        headerlist = []
     174
     175    cache.set(header_key, headerlist, cache_timeout)
     176    cache_key = _generate_cache_key(request, headerlist, key_prefix)
     177
     178    hashes_key = 'views.decorators.cache.cache_hashes.%s.%s' % (key_prefix, iri_to_uri(request.path))
     179    hashes = cache.get(hashes_key, [])
     180    page_hash = cache_key.split('.')[-1]
     181    if not page_hash in hashes:
     182        hashes.append(page_hash)
     183        cache.set(hashes_key, hashes, cache_timeout)
     184
     185    return cache_key
     186
     187def clear_cache_for_path(path, key_prefix=None):
     188    """
     189    Given a request path, every cache entry related to it gets deleted,
     190    specifically, the following keys get deleted:
     191        views.decorators.cache.cache_hashes.`key_prefix`.`path`
     192        views.decorators.cache.cache_header.`key_prefix`.`path`
     193        views.decorators.cache.cache_page.`key_prefix`.`path`.*
     194    """
     195    if key_prefix is None:
     196        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
     197    hashes_key = 'views.decorators.cache.cache_hashes.%s.%s' % (key_prefix, iri_to_uri(path))
     198    hashes = cache.get(hashes_key, None)
     199    if hashes:
     200        for page_hash in hashes:
     201            page_key = 'views.decorators.cache.cache_page.%s.%s.%s' \
     202                           % (key_prefix, iri_to_uri(path), page_hash)
     203            cache.delete(page_key)
     204        header_key = 'views.decorators.cache.cache_header.%s.%s' % (key_prefix, iri_to_uri(path))
     205        cache.delete(header_key)
     206    cache.delete(hashes_key)
     207
  • docs/cache.txt

     
    574574
    575575.. _`Cache-Control spec`: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
    576576
     577Cache refreshing
     578================
     579
     580It is possible to clear the cache for an specific view, to achieve that, you
     581should tell your view's url path (and optionally, a key_prefix) to
     582``clear_cache_for_path(path, key_prefix=None)``.
     583
     584    from django.utils.cache import clear_cache_for_path
     585   
     586    # clear the all cached data for path '/blog/posts/2/'
     587    clear_cache_for_path('/blog/posts/2/')
     588   
     589Note that it will delete every page cached from matching this path, ``Vary`` headers
     590doesn't matter.
     591
    577592Other optimizations
    578593===================
    579594
Back to Top