在Django中过期视图缓存? [英] Expire a view-cache in Django?

查看:175
本文介绍了在Django中过期视图缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@cache_page装饰器非常棒。但是对于我的博客,我想保留一个页面缓存,直到有人对一篇文章发表评论。这听起来像个好主意,因为人们很少发表评论,所以将这些页面保留在memcached中,而没有人会发表评论。我以为有人以前有这个问题吗?这与每个网址的缓存不同。

The @cache_page decorator is awesome. But for my blog I would like to keep a page in cache until someone comments on a post. This sounds like a great idea as people rarely comment so keeping the pages in memcached while nobody comments would be great. I'm thinking that someone must have had this problem before? And this is different than caching per url.

所以我想到的一个解决方案是:

So a solution I'm thinking of is:

@cache_page( 60 * 15, "blog" );
def blog( request ) ...

然后我会保留一个列表所有缓存键用于博客视图,然后有方式到期博客缓存空间。但是我并没有超级Django的经验,所以我想知道有没有人知道这样做的更好的方法?

And then I'd keep a list of all cache keys used for the blog view and then have way of expire the "blog" cache space. But I'm not super experienced with Django so I'm wondering if someone knows a better way of doing this?

推荐答案

我写了一个解决方案,就像我在自己的一些项目中所说的那样:

Here's a solution I wrote to do just what you're talking about on some of my own projects:

def expire_view_cache(view_name, args=[], namespace=None, key_prefix=None):
    """
    This function allows you to invalidate any view-level cache. 
        view_name: view function you wish to invalidate or it's named url pattern
        args: any arguments passed to the view function
        namepace: optioal, if an application namespace is needed
        key prefix: for the @cache_page decorator for the function (if any)
    """
    from django.core.urlresolvers import reverse
    from django.http import HttpRequest
    from django.utils.cache import get_cache_key
    from django.core.cache import cache
    # create a fake request object
    request = HttpRequest()
    # Loookup the request path:
    if namespace:
        view_name = namespace + ":" + view_name
    request.path = reverse(view_name, args=args)
    # get cache key, expire if the cached item exists:
    key = get_cache_key(request, key_prefix=key_prefix)
    if key:
        if cache.get(key):
            # Delete the cache entry.  
            #
            # Note that there is a possible race condition here, as another 
            # process / thread may have refreshed the cache between
            # the call to cache.get() above, and the cache.set(key, None) 
            # below.  This may lead to unexpected performance problems under 
            # severe load.
            cache.set(key, None, 0)
        return True
    return False

Django将这些缓存关联到视图请求中,所以这样做是为缓存视图创建一个假请求对象,使用它来获取缓存密钥,然后将其过期。

Django keys these caches of the view request, so what this does is creates a fake request object for the cached view, uses that to fetch the cache key, then expires it.

要按照您所说的方式使用它,请尝试以下操作:

To use it in the way you're talking about, try something like:

from django.db.models.signals import post_save
from blog.models import Entry

def invalidate_blog_index(sender, **kwargs):
    expire_view_cache("blog")

post_save.connect(invalidate_portfolio_index, sender=Entry)

所以基本上,当有一个博客Entry对象被保存时,invalidate_blog_index被调用,并且缓存的视图已经过期。注意:没有广泛测试,但到目前为止,这对我来说很好。

So basically, when ever a blog Entry object is saved, invalidate_blog_index is called and the cached view is expired. NB: haven't tested this extensively, but it's worked fine for me so far.

这篇关于在Django中过期视图缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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