简体   繁体   English

何时删除Django中的缓存条目

[英]when to delete the cache entry in django

In my django application ,I have a BlogEntry which belongs to a Category .A BlogEntry may belong to many Category s 在我的django应用程序中,我有一个BlogEntry属于一个Category 。一个BlogEntry可能属于许多Category

class BlogEntry(models.Model):
    creationdate=models.DateField(default=date.today)    
    description=models.TextField()
    author=models.ForeignKey(User,null=True)
    categories=models.ManyToManyField(Category)

class Category(models.Model):
    name=models.CharField(unique=True,max_length=50)
    description=models.TextField(blank=True)

A user may edit a BlogEntry and in doing so, may remove a Category it was in. 用户可以编辑BlogEntry并可以删除其中的Category

Suppose blogEntry1 belonged to java,scala before .If user edits it such that he removes scala .Now the entry has only one category ie java 假设blogEntry1java,scala之前属于java,scala 。如果用户对其进行编辑,以便他删除了scala 。现在该条目只有一个类别,即java

In my list_view I am using cache as below 在我的list_view中,我正在使用缓存,如下所示

from django.core.cache import cache
def list_entries_on_day(request,year,month,day):
    ...
    key = 'entries_day'+'-'+year+'-'+month+'-'+day
    if key not in cache:
        entries = BlogEntry.objects.filter(...args..)
        cache.set(key,entries)
    entries_on_day =cache.get(key)
    ...

Suppose I have created 2 entries for today and these are put in cache.If I edit one of these BlogEnty s and remove a category ie; 假设我今天已经创建了2个条目,并将它们放入缓存中。如果我编辑其中一个BlogEnty并删除类别,即;

blogEntry1  has categories :java,scala
blogEntry2 has categories :dotnet,vbasic

Initially I make a query for entries for today and put the result in cache 最初,我查询今天的条目并将结果放入缓存中

cache now has [blogEntry1,blogEntry2] against key 'entries_day-2012-11-11' 缓存现在针对键'entries_day-2012-11-11'具有[blogEntry1,blogEntry2]

Now I edit blogEntry1 such that it now has java as category 现在,我编辑blogEntry1 ,使其现在具有java作为类别

,do I need to remove the stored entries from cache?(Since the cache contains a BlogEntry object before it's modification) ,是否需要从缓存中删除存储的条目?(由于修改前缓存中包含BlogEntry对象)

You can invalidate cache by registering a signal handler for model.save 您可以通过为model.save注册信号处理程序来使缓存无效

You can also live with the fact that users will see the stale content until the cache expiration (1 hour default) make sure he logged in user will not see the cached content, otherwise he will honk the edit was lost. 您还可以忍受这样的事实,即用户将看到过时的内容,直到缓存过期(默认为1小时),以确保他登录的用户不会看到缓存的内容,否则,他将使编辑丢失。

Hmmm my answer is a bit vague, but I just wanted to say: no, you don't strictly have to invalidate cache at each edit, it is a choice between performance and content freshness. 嗯,我的回答有点含糊,但我只是想说:不,您不必在每次编辑时都必须使缓存无效,这是性能和内容新鲜度之间的选择。

One more nit: the preferred idiom for cache usage is: 另一点:缓存使用的首选习惯用法是:

entries_on_day = cache.get(key)
if entries_on_day  is None:
    entries_on_day  = BlogEntry.objects.filter(...args..)
    cache.set(key,entries_on_day)

You save one cache query 您保存一个缓存查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM