Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I expire a django template cache key on receiving a signal?

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?

like image 512
Hobhouse Avatar asked Nov 23 '09 10:11

Hobhouse


People also ask

Does Django automatically cache?

Local Memory Cache Unless we explicitly specify another caching method in our settings file, Django defaults to local memory caching. As its name implies, this method stores cached data in RAM on the machine where Django is running. Local memory caching is fast, responsive, and thread-safe.

What mechanism is used to enable caching for an individual view in Django?

Using Memcached One of the most popular and efficient types of cache supported natively by Django is MEMCACHED .

What is the default value of cull frequency for cache backend?

CULL_FREQUENCY : The fraction of entries that are culled when MAX_ENTRIES is reached. The actual ratio is 1 / CULL_FREQUENCY , so set CULL_FREQUENCY to 2 to cull half the entries when MAX_ENTRIES is reached. This argument should be an integer and defaults to 3 .

What are the caching strategies in Django file system caching?

To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.


3 Answers

Have a look at how the cache key is constructed:

args = md5_constructor(u':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on]))
cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest())

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 is d41d8cd98f00b204e9800998ecf8427e (the md5 hash of the empty string). The cache key should therefore end up to be

template.cache.my_posts.d41d8cd98f00b204e9800998ecf8427e

If you need a more general solution, this snippet might help.

like image 79
Benjamin Wohlwend Avatar answered Nov 15 '22 11:11

Benjamin Wohlwend


Note that the md5_constructor in the first line of Benjamin Wohlwend's example above is deprecated. Current (Nov. 2011) version is:

args = hashlib.md5(u':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on]))
like image 27
Jheasly Avatar answered Nov 15 '22 11:11

Jheasly


from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key

cache.delete(make_template_fragment_key('footer'))
like image 23
Yevgeniy Shchemelev Avatar answered Nov 15 '22 09:11

Yevgeniy Shchemelev