【问题标题】:django cache_page how to set versiondjango cache_page 如何设置版本
【发布时间】:2021-04-22 19:51:50
【问题描述】:

我可以通过cache.set设置版本:

cache.set(key, value, timeout=60, version=1)

但是如何通过 cache_page 装饰器来设置呢?

喜欢:

@cache_page(60, version=1)
def view(request):

【问题讨论】:

    标签: python django decorator


    【解决方案1】:

    django documentation 提到缓存装饰器只能接受一个参数和 2 个可选参数,它们都不用于版本控制,恐怕您将不得不使用缓存函数进行版本控制或尝试将自己的功能添加到装饰器.

    编辑: 设置版本的唯一方法是使用

    incr_version('my_key') 
    

    decr_version('my_key')
    

    【讨论】:

      【解决方案2】:

      您必须在 settings.py 中向 CACHES(您必须添加字典)字典添加一个条目:

      CACHES = {
          'default': {
              'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
          },
          'my_cache': {
              'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
              'VERSION': 1 # Or of your preference
          }
      }
      

      现在在你的装饰器中你可以指定:

      @cache_page(60, cache="my_cache")
      def view(request):
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        • 2013-11-14
        • 2022-07-20
        • 2021-10-26
        • 2015-05-24
        • 1970-01-01
        • 2011-10-03
        相关资源
        最近更新 更多