如何在Django中使用2个不同的缓存后端?

时间:2022-03-23 02:27:53

I need to use memcached and file based cache. I setup my cache in settings:

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

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

dummy is temporary. Docs says:

假人是暂时的。文件说:

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

OK, but how can I now set and get cache only for 'inmem' cache backend (in future memcached)? Documentation doesn't mention how to do that.

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

4 个解决方案

#1


25  

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


8  

Since Django 1.9, get_cache is deprecated. Do the following to address keys from 'inmem' (addition to answer by Romans):

从Django 1.9开始,不推荐使用get_cache。执行以下操作来解决“inmem”中的键(除了Romans的回答):

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

#3


2  

In addition to Romans's answer above... You can also conditionally import a cache by name, and use the default (or any other cache) if the requested doesn't exist.

除了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


-2  

Unfortunately, you can't change which cache alias is used for the low-level cache.set() and cache.get() methods.

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

These methods always use 'default' cache as per line 51 (in Django 1.3) of django.core.cache.__init__.py:

根据django.core.cache.__init__.py的第51行(在Django 1.3中),这些方法总是使用'​​default'缓存:

DEFAULT_CACHE_ALIAS = 'default'

So you need to set your 'default' cache to the cache you want to use for the low-level cache and then use the other aliases for things like site cache, page cache, and db cache routing. `

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

#1


25  

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


8  

Since Django 1.9, get_cache is deprecated. Do the following to address keys from 'inmem' (addition to answer by Romans):

从Django 1.9开始,不推荐使用get_cache。执行以下操作来解决“inmem”中的键(除了Romans的回答):

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

#3


2  

In addition to Romans's answer above... You can also conditionally import a cache by name, and use the default (or any other cache) if the requested doesn't exist.

除了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


-2  

Unfortunately, you can't change which cache alias is used for the low-level cache.set() and cache.get() methods.

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

These methods always use 'default' cache as per line 51 (in Django 1.3) of django.core.cache.__init__.py:

根据django.core.cache.__init__.py的第51行(在Django 1.3中),这些方法总是使用'​​default'缓存:

DEFAULT_CACHE_ALIAS = 'default'

So you need to set your 'default' cache to the cache you want to use for the low-level cache and then use the other aliases for things like site cache, page cache, and db cache routing. `

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