简体   繁体   English

Django-禁用用户的页面级缓存

[英]Django - Disable Page Level Caching by User

I've set up page level caching for many of our pages. 我已经为我们的许多页面设置了页面级缓存。 However once in a while an admin user logs in to preview potential changes to the site. 但是,管理员用户有时会登录以预览对该站点的潜在更改。

Is there a way to disable page level caching just for these users? 有没有一种方法可以仅针对这些用户禁用页面级缓存?

I read through the docs but I didn't see anything. 我阅读了文档,但没有看到任何内容。

Update: Here's my attempt based on v1k45's answer: 更新:这是我基于v1k45的回答:

from django.middleware.cache import FetchFromCacheMiddleware


logger = logging.getLogger(__name__)


class ExceptImpersonateFetchFromCacheMiddleware(FetchFromCacheMiddleware):
    def process_request(self, request):
        # Break out of caching is we're using impersonate
        if request.user and hasattr(request.user, 'is_impersonate') and request.user.is_impersonate:
            logger.warning("Skipping cache_page for user %s because it is impersonation" % str(request.user))
            request._cache_update_cache = False
            return None
        # Normal flow:
        return super(ExceptImpersonateFetchFromCacheMiddleware, self).process_request(request)

You can extend the CacheMiddleware provided by django such that the admin users always see fresh content instead of cached. 您可以扩展django提供的CacheMiddleware,以便管理员用户始终看到新鲜的内容,而不是缓存的内容。

Have a look at the source code for FetchFromCacheMiddleware , you can see this code snippet: 看一下FetchFromCacheMiddleware的源代码,您可以看到以下代码片段:

def process_request(self, request):
    [...]
    if request.method not in ('GET', 'HEAD'):
        request._cache_update_cache = False
        return None  # Don't bother checking the cache.

The if condition here tells django to skip cache and don't update the existing cached data if the request method is not GET or HEAD . 如果请求方法不是GETHEAD ,则if条件告诉django跳过缓存并且不更新现有的缓存数据。

Similarly, you can add a check where you skip the cache if the user is an admin. 同样,如果用户是管理员,则可以在跳过缓存的位置添加检查。 Roughly it will look like this: 大致如下所示:

def process_request(self, request):
    [...snip..]
    if request.user.is_staff:
        request._cache_update_cache = False
        return None  # Don't bother checking the cache.

UPDATE : The cache_page decorator uses django's CacheMiddleware which extends the functionality of FetchFromCacheMiddleware and UpdateCacheMiddleware. 更新cache_page装饰器使用Django的CacheMiddleware ,它扩展了FetchFromCacheMiddleware和UpdateCacheMiddleware的功能。

Now you'll have to make your own version of CacheMiddleware and cache_page decorator. 现在,您必须制作自己的CacheMiddlewarecache_page装饰器版本。 This custom_cache_page decorator will call your CustomCacheMiddleware which extends your CustomFetchFromCacheMiddleware and django's UpdateCacheMiddleware . 这个custom_cache_page装饰器将调用您的CustomCacheMiddleware ,它扩展了CustomFetchFromCacheMiddleware和Django的UpdateCacheMiddleware

After you have completed the CustomCacheMiddleware, you'll have to replace django's CacheMiddleware with your own. 完成CustomCacheMiddleware之后,您必须用自己的django的CacheMiddleware替换。 This can be done by changing MIDDLEWARE_CLASSES tuple in settings.py. 这可以通过更改settings.py中的MIDDLEWARE_CLASSES元组来完成。

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

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