【问题标题】:How to use 2 different cache backends in Django?如何在 Django 中使用 2 个不同的缓存后端?
【发布时间】:2011-05-10 11:25:14
【问题描述】:

我需要使用 memcached 和基于文件的缓存。 我在设置中设置了我的缓存:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': 'c:/foo/bar',
    },
    'inmem': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}

dummy 是暂时的。 文档说:

cache.set('my_key', 'hello, world!', 30)
cache.get('my_key')

好的,但是我现在如何设置和获取仅为“inmem”缓存后端(将来的 memcached)的缓存?文档没有提到如何做到这一点。

【问题讨论】:

    标签: django django-cache django-caching


    【解决方案1】:
    CACHES = {
      'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': 'c:/foo/bar',
      },
      'inmem': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
      }
    } 
    
    from django.core.cache import get_cache, cache
    inmem_cache = get_cache('inmem')
    default_cache = get_cache('default')
    # default_cache == cache 
    

    【讨论】:

      【解决方案2】:

      从 Django 1.9 开始,get_cache 已被弃用。执行以下操作来解决来自“inmem”的键(罗马人回答的补充):

      from django.core.cache import caches
      caches['inmem'].get(key)
      

      【讨论】:

        【解决方案3】:

        除了上面 Romans 的回答...您还可以按名称有条件地导入缓存,如果请求的不存在,则使用默认(或任何其他缓存)。

        from django.core.cache import cache as default_cache, get_cache
        from django.core.cache.backends.base import InvalidCacheBackendError
        
        try:
            cache = get_cache('foo-cache')
        except InvalidCacheBackendError:
            cache = default_cache
        
        cache.get('foo')
        

        【讨论】:

          【解决方案4】:

          From the docs:

          >>> from django.core.cache import caches
          >>> cache1 = caches['myalias']
          >>> cache2 = caches['myalias']
          >>> cache1 is cache2
          True
          

          【讨论】:

            【解决方案5】:

            创建一个名为 get_cache 的实用程序函数。其他答案中引用的 get_cache 方法在某些 django 版本的 django.core.cache 库中不存在。改为使用以下内容

            from django.utils.connection import ConnectionProxy
            from django.core.cache import caches
            
            def get_cache(alias):
              return ConnectionProxy(caches, alias)
            
            
            cache = get_cache('infile')
            value = cache.get(key)
            

            【讨论】:

              【解决方案6】:

              很遗憾,您无法更改用于低级 cache.set()cache.get() 方法的缓存别名。

              根据django.core.cache.__init__.py 的第 51 行(在 Django 1.3 中),这些方法始终使用“默认”缓存:

              DEFAULT_CACHE_ALIAS = 'default'
              

              因此,您需要将“默认”缓存设置为要用于低级缓存的缓存,然后将其他别名用于站点缓存、页面缓存和数据库缓存路由等内容。 `

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2011-11-15
                • 2015-02-03
                • 2016-09-24
                • 2016-08-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多