In my front page template I use the cache function like this:
{% cache 86400 my_posts %}
{% get_latest_posts %}
{% endcache %}
When there is a new post I would like to expire the cache key; like this:
def clear_post_cache():
cache.delete('my_posts')
post_save.connect(clear_post_cache, sender=Post)
My problem is that the cache key isn't accessible as 'my_posts'. How do I find the key name?
Have a look at how the cache key is constructed:
The key is a combination of the fragment name (
my_posts
) and a md5 sum of additional arguments to the cache tag. Since you don't have additional arguments, the hexdigest isd41d8cd98f00b204e9800998ecf8427e
(the md5 hash of the empty string). The cache key should therefore end up to beIf you need a more general solution, this snippet might help.
Note that the
md5_constructor
in the first line of Benjamin Wohlwend's example above is deprecated. Current (Nov. 2011) version is: