如何在接收到信号时过期django模板缓存键? [英] How do I expire a django template cache key on receiving a signal?

查看:140
本文介绍了如何在接收到信号时过期django模板缓存键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的首页模板中,我使用这样的缓存功能:

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)

我的问题是缓存键isn'可以作为'my_posts'访问。如何找到密钥名称?

My problem is that the cache key isn't accessible as 'my_posts'. How do I find the key name?

推荐答案

看看缓存密钥如何构造

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

关键是片段名称( my_posts )与缓存标签的附加参数的md5和的组合。由于您没有其他参数,所以hexdigest是 d41d8cd98f00b204e9800998ecf8427e (空字符串的md5哈希)。因此,缓存密钥最终应该是

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.

这篇关于如何在接收到信号时过期django模板缓存键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆