我需要使用memcached和基于文件的缓存。我在设置中设置了我的缓存:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': 'c:/foo/bar',
},
'inmem': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
dummy是临时的。Docs说:
cache.set('my_key', 'hello, world!', 30)
cache.get('my_key')
好吧,但我现在如何设置和获取缓存只为'inmem‘缓存后端(在未来的memcached)?文档中没有提到如何做到这一点。
发布于 2011-05-19 02:43:17
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
发布于 2016-11-29 01:49:45
从Django 1.9开始,get_cache
就被废弃了。执行以下操作以从'inmem‘中寻址密钥(罗马人对answer的补充):
from django.core.cache import caches
caches['inmem'].get(key)
发布于 2014-03-20 05:25:05
除了罗马人上面的回答。您还可以按名称有条件地导入缓存,如果请求的缓存不存在,则使用默认缓存(或任何其他缓存)。
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')
https://stackoverflow.com/questions/5949271
复制相似问题