繁体   English   中英

使 Django 中的视图缓存过期?

[英]Expire a view-cache in Django?

@cache_page decorator很棒。 但是对于我的博客,我想在缓存中保留一个页面,直到有人对帖子发表评论。 这听起来是个好主意,因为人们很少发表评论,因此将页面保存在 memcached 中而没有人发表评论会很棒。 我在想之前一定有人遇到过这个问题吗? 这与按 url 缓存不同。

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

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

然后我会保留用于博客视图的所有缓存键的列表,然后让“博客”缓存空间过期。 但是我对 Django 并不是非常有经验,所以我想知道是否有人知道这样做的更好方法?

此解决方案适用于 1.7 之前的 django 版本

这是我写的一个解决方案,用于在我自己的一些项目中执行您所说的操作:

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 对视图请求的这些缓存进行键控,因此它的作用是为缓存视图创建一个假请求对象,使用它来获取缓存键,然后使其过期。

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

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)

所以基本上,当一个 blog Entry 对象被保存时, invalidate_blog_index 被调用并且缓存的视图过期。 注意:尚未对此进行广泛测试,但到目前为止对我来说效果很好。

我为这种情况编写了Django-groupcache (你可以在这里下载代码)。 在你的情况下,你可以写:

from groupcache.decorators import cache_tagged_page

@cache_tagged_page("blog", 60 * 15)
def blog(request):
    ...

从那里,您可以稍后简单地执行以下操作:

from groupcache.utils import uncache_from_tag

# Uncache all view responses tagged as "blog"
uncache_from_tag("blog") 

也看看 cache_page_against_model() :它稍微复杂一点,但它允许您根据模型实体更改自动取消缓存响应。

cache_page 装饰器最终将使用 CacheMiddleware,它将根据请求(查看django.utils.cache.get_cache_key )和 key_prefix (在您的情况下为“博客”)生成一个缓存键。 请注意,“blog”只是一个前缀,而不是整个缓存键。

您可以在保存评论时通过django 的 post_save 信号获得通知,然后您可以尝试为适当的页面构建缓存键,最后说cache.delete(key)

然而,这需要 cache_key,它是根据对先前缓存的视图的请求构造的。 保存评论时,此请求对象不可用。 您可以在没有正确请求对象的情况下构造缓存键,但这种构造发生在标记为私有 ( _generate_cache_header_key ) 的函数中,因此您不应直接使用此函数。 但是,您可以构建一个具有与原始缓存视图相同的路径属性的对象,Django 不会注意到,但我不建议这样做。

cache_page 装饰器为您抽象了缓存,并且很难直接删除某个缓存对象。 您可以创建自己的键并以相同的方式处理它们,但这需要更多的编程并且不像cache_page装饰器那样抽象。

当您的评论显示在多个视图中时,您还必须删除多个缓存对象(即带有评论计数的索引页面和单个博客条目页面)。

总结一下:Django 为你做基于时间的缓存键过期,但是在正确的时间自定义删除缓存键更棘手。

使用最新版本的 Django(>=2.0),您正在寻找的东西很容易实现:

from django.utils.cache import learn_cache_key
from django.core.cache import cache
from django.views.decorators.cache import cache_page

keys = set()

@cache_page( 60 * 15, "blog" );
def blog( request ):
    response = render(request, 'template')
    keys.add(learn_cache_key(request, response)
    return response

def invalidate_cache()
    cache.delete_many(keys)

当有人通过 pre_save 信号更新博客中的帖子时,您可以将 invalidate_cache 注册为回调。

这不适用于 django 1.7; 正如你在这里看到的https://docs.djangoproject.com/en/dev/releases/1.7/#cache-keys-are-now-generated-from-the-request-s-absolute-url新的缓存键是使用完整 URL 生成,因此仅路径的虚假请求将不起作用。 您必须正确设置请求主机值。

fake_meta = {'HTTP_HOST':'myhost',}
request.META = fake_meta

如果您有多个域使用相同的视图,您应该在 HTTP_HOST 中循环它们,获取正确的密钥并对每个域进行清理。

v1.7 及更高版本的 Django 视图缓存失效。 在 Django 1.9 上测试。

def invalidate_cache(path=''):
    ''' this function uses Django's caching function get_cache_key(). Since 1.7, 
        Django has used more variables from the request object (scheme, host, 
        path, and query string) in order to create the MD5 hashed part of the
        cache_key. Additionally, Django will use your server's timezone and 
        language as properties as well. If internationalization is important to
        your application, you will most likely need to adapt this function to
        handle that appropriately.
    '''
    from django.core.cache import cache
    from django.http import HttpRequest
    from django.utils.cache import get_cache_key

    # Bootstrap request:
    #   request.path should point to the view endpoint you want to invalidate
    #   request.META must include the correct SERVER_NAME and SERVER_PORT as django uses these in order
    #   to build a MD5 hashed value for the cache_key. Similarly, we need to artificially set the 
    #   language code on the request to 'en-us' to match the initial creation of the cache_key. 
    #   YMMV regarding the language code.        
    request = HttpRequest()
    request.META = {'SERVER_NAME':'localhost','SERVER_PORT':8000}
    request.LANGUAGE_CODE = 'en-us'
    request.path = path

    try:
        cache_key = get_cache_key(request)
        if cache_key :
            if cache.has_key(cache_key):
                cache.delete(cache_key)
                return (True, 'successfully invalidated')
            else:
                return (False, 'cache_key does not exist in cache')
        else:
            raise ValueError('failed to create cache_key')
    except (ValueError, Exception) as e:            
        return (False, e)

用法:

status, message = invalidate_cache(path='/api/v1/blog/')

FWIW 我不得不修改 mazelife 的解决方案才能让它工作:

def expire_view_cache(view_name, args=[], namespace=None, key_prefix=None, method="GET"):
    """
    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: http://stackoverflow.com/questions/2268417/expire-a-view-cache-in-django
        added: method to request to get the key generating properly
    """
    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()
    request.method = method
    # 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):
            cache.set(key, None, 0)
        return True
    return False

如果没有评论,您可以手动缓存博客文章对象(或类似的),而不是使用缓存页面装饰器,然后当有第一条评论时,重新缓存博客文章对象,以便它是最新的(假设对象具有引用任何评论的属性),但是只需让评论博客文章的缓存数据过期,然后就不用再重新缓存了...

我有同样的问题,我不想弄乱 HTTP_HOST,所以我创建了自己的 cache_page 装饰器:

from django.core.cache import cache


def simple_cache_page(cache_timeout):
    """
    Decorator for views that tries getting the page from the cache and
    populates the cache if the page isn't in the cache yet.

    The cache is keyed by view name and arguments.
    """
    def _dec(func):
        def _new_func(*args, **kwargs):
            key = func.__name__
            if kwargs:
                key += ':' + ':'.join([kwargs[key] for key in kwargs])

            response = cache.get(key)
            if not response:
                response = func(*args, **kwargs)
                cache.set(key, response, cache_timeout)
            return response
        return _new_func
    return _dec

要过期的页面缓存只需要调用:

cache.set('map_view:' + self.slug, None, 0)

其中 self.slug - 来自 urls.py 的参数

url(r'^map/(?P<slug>.+)$', simple_cache_page(60 * 60 * 24)(map_view), name='map'), 

Django 1.11,Python 3.4.3

每次有人评论帖子时,您都可以使用新的“key_prefix”代替显式缓存过期。 例如,它可能是最后一篇文章评论的日期时间(您甚至可以将此值与Last-Modified标头结合起来)。

不幸的是 Django(包括cache_page() )不支持动态“key_prefix”es(在Django 1.9上检查)但存在解决方法。 您可以实现自己的cache_page() ,它可以使用包含动态“key_prefix”支持的扩展CacheMiddleware 例如:

from django.middleware.cache import CacheMiddleware
from django.utils.decorators import decorator_from_middleware_with_args

def extended_cache_page(cache_timeout, key_prefix=None, cache=None):
    return decorator_from_middleware_with_args(ExtendedCacheMiddleware)(
        cache_timeout=cache_timeout,
        cache_alias=cache,
        key_prefix=key_prefix,
    )

class ExtendedCacheMiddleware(CacheMiddleware):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if callable(self.key_prefix):
            self.key_function = self.key_prefix

    def key_function(self, request, *args, **kwargs):
        return self.key_prefix

    def get_key_prefix(self, request):
        return self.key_function(
            request,
            *request.resolver_match.args,
            **request.resolver_match.kwargs
        )

    def process_request(self, request):
        self.key_prefix = self.get_key_prefix(request)
        return super().process_request(request)

    def process_response(self, request, response):
        self.key_prefix = self.get_key_prefix(request)
        return super().process_response(request, response)

然后在你的代码中:

from django.utils.lru_cache import lru_cache

@lru_cache()
def last_modified(request, blog_id):
    """return fresh key_prefix"""

@extended_cache_page(60 * 15, key_prefix=last_modified)
def view_blog(request, blog_id):
    """view blog page with comments"""

邓肯的回答适用于 Django 1.9。 但是如果我们需要使用 GET 参数使 url 无效,我们必须对请求进行一些更改。 例如对于 .../?mykey=myvalue

request.META = {'SERVER_NAME':'127.0.0.1','SERVER_PORT':8000, 'REQUEST_METHOD':'GET', 'QUERY_STRING': 'mykey=myvalue'}
request.GET.__setitem__(key='mykey', value='myvalue')

我遇到了类似的情况,这是我想出的解决方案,我在早期版本的 Django 上启动它,但它目前在 2.0.3 版上使用。

第一个问题:当您设置要在 Django 中缓存的内容时,它会设置标头,以便下游缓存(包括浏览器缓存)缓存您的页面。

要覆盖它,您需要设置中间件。 我从 StackOverflow 上的其他地方抄录了这个,但目前找不到。 appname/middleware.py

from django.utils.cache import add_never_cache_headers


class Disable(object):

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        add_never_cache_headers(response)
        return response

然后在settings.py ,在MIDDLEWARE中添加:

'appname.middleware.downstream_caching.Disable',

请记住,这种方法完全禁用了下游缓存,这可能不是您想要的。

最后,我添加到我的views.py

def expire_page(request, path=None, query_string=None, method='GET'):
    """
    :param request: "real" request, or at least one providing the same scheme, host, and port as what you want to expire
    :param path: The path you want to expire, if not the path on the request
    :param query_string: The query string you want to expire, as opposed to the path on the request
    :param method: the HTTP method for the page, if not GET
    :return: None
    """
    if query_string is not None:
        request.META['QUERY_STRING'] = query_string
    if path is not None:
        request.path = path
    request.method = method

    # get_raw_uri and method show, as of this writing, everything used in the cache key
    # print('req uri: {} method: {}'.format(request.get_raw_uri(), request.method))
    key = get_cache_key(request)
    if key in cache:
        cache.delete(key)

我不喜欢必须传入request对象,但在撰写本文时,它提供了request的方案/协议、主机和端口,您的站点/应用程序的几乎任何请求对象都可以,只要您传入路径和查询字符串。

Duncan 答案的另一个更新版本:必须找出正确的元字段:(在 Django 1.9.8 上测试)

def invalidate_cache(path=''):
    import socket
    from django.core.cache import cache
    from django.http import HttpRequest
    from django.utils.cache import get_cache_key

    request = HttpRequest()
    domain = 'www.yourdomain.com'
    request.META = {'SERVER_NAME': socket.gethostname(), 'SERVER_PORT':8000, "HTTP_HOST": domain, 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br'}
    request.LANGUAGE_CODE = 'en-us'
    request.path = path

    try:
        cache_key = get_cache_key(request)
        if cache_key :
            if cache.has_key(cache_key):
                cache.delete(cache_key)
                return (True, 'successfully invalidated')
            else:
                return (False, 'cache_key does not exist in cache')
        else:
            raise ValueError('failed to create cache_key')
    except (ValueError, Exception) as e:            
        return (False, e)

上面的大多数解决方案在我们的案例中都不起作用,因为我们使用https get_cache_key的源代码显示它使用request.get_absolute_uri()来生成缓存键。

默认的HttpRequest类将scheme设置为http 因此,我们需要覆盖它以将https用于我们的虚拟请求对象。

这是对我们来说很好用的代码:)

from django.core.cache import cache
from django.http import HttpRequest
from django.utils.cache import get_cache_key


class HttpsRequest(HttpRequest):
    @property
    def scheme(self):
        return "https"


def invalidate_cache_page(
    path,
    query_params=None,
    method="GET",
):
    request = HttpsRequest()

    # meta information can be checked from error logs
    request.META = {
        "SERVER_NAME": "www.yourwebsite.com",
        "SERVER_PORT": "443",
        "QUERY_STRING": query_params,
    }

    request.path = path
    key = get_cache_key(request, method=method)
    if cache.has_key(key):
        cache.delete(key)

现在我可以使用这个实用函数从我们的任何视图中使缓存无效:

page = reverse('url_name', kwargs={'id': obj.id})
invalidate_cache_page(path)

它现在更简单(在Django 1.10上测试)

from django.db.models.signals import post_save
from django.core.cache import cache
from django.dispatch import receiver

@receiver(post_save)
def clear_the_cache(**kwargs):
    cache.clear()

解决方案很简单,不需要任何额外的工作。

例子

@cache_page(60 * 10)
def our_team(request, sorting=None):
    ...

这将使用默认键设置对缓存的响应。

使视图缓存过期

from django.utils.cache import get_cache_key
from django.core.cache import cache

def our_team(request, sorting=None):
    # This will remove the cache value and set it to None
    cache.set(get_cache_key(request), None)

简单,干净,快速。

暂无
暂无

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

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