Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - expire django template cache based on variables

Its not to hard to invalidate a particular template cache in django

def invalidate_cache_key(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)

However I have a situation where I need to delete all cached fragments that have had a certain variable passed to them. For example, delete all cached fragments about the car brand 'Toyota'.

{% cache 100000 car_content car.brand %}

Essentially is there a way to get all cache_keys based on a certain set of criteria? I have thought dangerously about changing the cache source but I was wondering if there might be a better solution to this problem.

like image 478
Werda Avatar asked Nov 14 '22 05:11

Werda


1 Answers

I do this with caching namespaces. Here is a decent explanation:

http://blog.dberg.org/2008/07/user-based-memcached-namespaces.html

like image 101
Tyler Avatar answered Dec 09 '22 18:12

Tyler