invalidation of cache-template-tag cache

1
2
3
4
5
6
7
8
9
from django.core.cache import cache
from django.utils.hashcompat import md5_constructor
from django.utils.http import urlquote
from functional import compose

def invalidate_template_cache(fragment_name, *variables):
    args = md5_constructor(u':'.join(apply(compose(urlquote, unicode), variables)))
    cache_key = 'template.cache.%s.%s' % (fragment_name, args.hexdigest())
    cache.delete(cache_key)

More like this

  1. Delete template fragment cache by DeRomanok 1 year, 1 month ago
  2. Cached template filters by coleifer 3 years ago
  3. Parsed RSS into template var by bram 4 years, 10 months ago
  4. Url filter middleware by limodou 6 years, 2 months ago
  5. render_as_template template tag by cogat 4 years, 2 months ago

Comments

bram (on June 24, 2009):

ah, my bad... "functional" is http://oakwinter.com/code/functional/

For completeness sake:

def compose(func_1, func_2, unpack=False):
    """
    compose(func_1, func_2, unpack=False) -> function

    The function returned by compose is a composition of func_1 and func_2.
    That is, compose(func_1, func_2)(5) == func_1(func_2(5))
    """
    if not callable(func_1):
        raise TypeError("First argument to compose must be callable")
    if not callable(func_2):
        raise TypeError("Second argument to compose must be callable")

    if unpack:
        def composition(*args, **kwargs):
            return func_1(*func_2(*args, **kwargs))
    else:
        def composition(*args, **kwargs):
            return func_1(func_2(*args, **kwargs))
    return composition

#

wiggins (on September 7, 2009):

This didn't work for me on django1.1,

The following did though:

def invalidate_template_cache(fragment_name, *variables):
  args = md5_constructor(u':'.join([urlquote(var) for var in variables]))
  cache_key = 'template.cache.%s.%s' % (fragment_name, args.hexdigest())
  cache.delete(cache_key)

#

tedtieken (on December 18, 2011):

https://github.com/tedtieken/django-template-reset-cache

Wraps the invalidation function in a template tag.

#

(Forgotten your password?)