为了账号安全,请及时绑定邮箱和手机立即绑定

Django 1.11:为经过身份验证的用户禁用缓存

Django 1.11:为经过身份验证的用户禁用缓存

MM们 2023-03-08 14:23:38
我们有一个用 python 2.7 和 django 1.11 编写的遗留应用程序(没有要迁移的资源)。它还用于grappelli授权。我们尝试Edit为一些页面添加链接(每个页面显示一个Round对象的详细信息),这些页面应该只对有权编辑回合的授权用户可见(APPNAME | round | Can change round在grappelli网络界面中)。在模板中,权限检查如下:{% if perms.round.can_change_round %}    &emsp;<a href="{{link_to_change_round}}" class="stuff-only-link">{% trans 'Edit' %}</a>{% endif %}当以下事件在很短的时间间隔内发生时,就会出现问题:有权编辑回合的用户访问页面 - 并看到链接Edit。无权编辑回合的用户(例如匿名用户)访问同一页面 - 并且还看到了链接!相关设置 ( settings.py) 是:CACHES = {    'default': {    #   'BACKEND': 'django.core.cache.backends.dummy.DummyCache',        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',    }}SOLO_CACHE = 'default'SOLO_CACHE_TIMEOUT = 5*60当我将缓存更改为 时dummy,问题就消失了。因此,完全禁用授权用户的缓存似乎是一个显而易见的解决方案。更准确地说:a) 如果用户是匿名的(大多数真实站点用户)——请求的页面可以写入缓存,也可以从缓存中读取;b) 如果一个用户被授权(大约 5-7 个用户)——请求的页面不能写入缓存也不能从缓存中读取。我如何实现这一目标?
查看完整描述

2 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

您可以对模板的缓存部分进行分段,并从与用户相关的缓存部分中省略,或者根据记录的片段变量状态缓存它们


查看完整回答
反对 回复 2023-03-08
?
临摹微笑

TA贡献1982条经验 获得超2个赞

经过一个小时的谷歌搜索后,找到并调整了答案。代码是:


编辑:最初,缓存是基于功能的。因此,“/rounds/1”给出了与“/rounds/2”相同的(缓存的)值。我们将完整的 URL 添加到缓存键来解决这个问题。


# -*- encoding: utf-8 -*-

'''

Python >= 2.4

Django >= 1.0


Author: eu@rafaelsdm.com

'''

# https://djangosnippets.org/snippets/2524/

# https://stackoverflow.com/questions/20146741/django-per-user-view-caching

# https://stackoverflow.com/questions/62913281/django-1-11-disable-cache-for-authentificated-users


from django.core.cache import cache


def cache_per_user(ttl=None, prefix=None):

    '''Decorador que faz cache da view pra cada usuario

    * ttl - Tempo de vida do cache, não enviar esse parametro significa que o

      cache vai durar até que o servidor reinicie ou decida remove-lo 

    * prefix - Prefixo a ser usado para armazenar o response no cache. Caso nao

      seja informado sera usado 'view_cache_'+function.__name__

    * cache_post - Informa se eh pra fazer cache de requisicoes POST

    * O cache para usuarios anonimos é compartilhado com todos

    * A chave do cache será uma das possiveis opcoes:

        '%s_%s'%(prefix, user.id)

        '%s_anonymous'%(prefix)

        'view_cache_%s_%s'%(function.__name__, user.id)

        'view_cache_%s_anonymous'%(function.__name__)

    '''

    def decorator(function):

        def apply_cache(request, *args, **kwargs):


            # No caching for authorized users:

            # they have to see the results of their edits immideately!


            can_cache = request.user.is_anonymous() and request.method == 'GET'


            # Gera a chave do cache

            if prefix:

                CACHE_KEY = '%s_%s'%(prefix, 'anonymous')

            else:

                CACHE_KEY = 'view_cache_%s_%s_%s'%(function.__name__, request.get_full_path(), 'anonymous')


            if can_cache:

                response = cache.get(CACHE_KEY, None)

            else:

                response = None


            if not response:

                print 'Not in cache: %s'%(CACHE_KEY)

                response = function(request, *args, **kwargs)

                if can_cache:

                    cache.set(CACHE_KEY, response, ttl)

            return response

        return apply_cache

    return decorator

然后在views.py:


from cache_per_user import cache_per_user as cache_page

#...

#

#

@cache_page(cache_duration)

def round_detail(request, pk):


查看完整回答
反对 回复 2023-03-08
  • 2 回答
  • 0 关注
  • 112 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号