跳转到主要内容

懒Django片段缓存清理

项目描述

使用模型版本令牌作为缓存键的前缀来执行片段缓存无效化。版本令牌可以是内部memcached计数器或模型的带时间戳的属性,如updated_at

安装

使用pip或使用python setup.py install安装,并将‘cachesweeper’添加到您的settings.INSTALLED_APPS

post_save缓存清理器

一个示例设置;一篇文章有很多评论,每个评论都进行缓存,单个投票应该使评论的特定缓存片段以及整篇文章的页面失效。

模板片段缓存

{% cachesweeper %}将其第一个参数作为Django ORM模型,第二个参数作为过期时间,任何后续参数都用于构建缓存键的其余部分

{% load markup %}
{% load cachesweeper_tags %}
{% cachesweeper comment 500 "comment.xml" %}
<p>
    <strong>{{comment.user}}</strong> said at {{comment.created_at}}:<br/>
    {{comment.content|markdown}}
    <br/>
    <a href={% url like article_id=article.pk,comment_id=comment.pk %}>Like ({{comment.likes.count}})</a>
    <a href={% url dislike article_id=article.pk,comment_id=comment.pk %}>Dislike ({{comment.dislikes.count}})</a>
</p>
{% endcachesweeper %}

当模型发生变化时使片段无效

在post_save中使给定模型的缓存无效。有两种选择,要么让Memcached为每个模型保持内部版本计数器,要么使用关键字using作为缓存版本化的手段。

from cachesweeper.utils import invalidate_cache_for

# using Memcached's internal counter
def invalidate_vote_cache(sender, **kwargs):
    vote = kwargs.get('instance')
    invalidate_cache_for(vote.comment)

post_save.connect(invalidate_vote_cache, sender=Vote)


# using a model's attribute
def invalidate_article_cache(sender, **kwargs):
    article = kwargs.get('instance')
    invalidate_cache_for(article, using='updated_at')

post_save.connect(invalidate_article_cache, sender=Article)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。

源分发

django-cache-sweeper-0.1.2.tar.gz (6.2 kB 查看哈希值)

上传时间

构建分发

django_cache_sweeper-0.1.2-py2.6.egg (14.8 kB 查看哈希值)

上传时间

由以下支持