Login

invalidation of cache-template-tag cache

Author:
bram
Posted:
June 24, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

this function invalidates a template-fragment cache bit.

say you have:

{% load cache %} {% cache 600 user_cache user.id %} something expensive here {% endcache %}

maybe you want to force an update. With this function you can, just call:

invalidate_template_cache("user_cache", user.id)

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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

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.

#

Please login first before commenting.