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

后端 未结 3 1848
一生所求
一生所求 2021-02-06 10:25

In my front page template I use the cache function like this:

{% cache 86400 my_posts %}
    {% get_latest_posts %}
{% endcache %}

When there i

3条回答
  •  礼貌的吻别
    2021-02-06 11:09

    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.

提交回复
热议问题