我正在我的 Windows 系统上运行 memcached 服务,并且我已将我的开发设置配置为具有以下缓存设置:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'TIMEOUT': 3600,
'OPTIONS': {
'MAX_ENTRIES': 100
}
}
}
我正在使用以下代码在缓存中设置和获取内容:
from django.core.cache import cache
def get_element(fsid):
element = cache.get(str(fsid)) # get element from memcache if present
if element is None:
info("cache miss for fsid-"+str(fsid))
fs = FlowStatusModel.objects.get(pk=fsid)
pickle_path = fs.pickle
gcs_pickle_path = fs.gcs_pickle
try:
info("reading from local disk")
with open(pickle_path, 'rb') as handle:
element = pickle.load(handle)
except:
info("local disk failed. copying file from cloud storage bucket to local disk")
create_and_copy(gcs_pickle_path, pickle_path)
with open(gcs_pickle_path, 'rb') as handle:
element = pickle.load(handle)
info("adding fsid-"+str(fsid) + " to cache with 1hour timeout")
cache.set(str(fsid), element, 3600)
return element
我在日志中看到总是有缓存未命中。我不知道 django 是否能够在缓存中设置元素。如果我尝试python manage.py shell
使用简单的set
and get
,我可以取回数据。我尝试memcached.exe -vv
从命令提示符运行命令以查看它是否接收到请求,但在这两种情况下(来自 dev server/manage.py shell)我都看到任何设置或获取控制台上打印的信息。感谢您在解决问题方面的任何帮助。