我有一个关于在Django项目中使用memcached设置缓存的问题。
在settings.py文件中,我有:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'TIMEOUT': 120
}
}
它可以工作,但是django中的默认session_engine是 django.contrib.sessions.backends.db ,但是当我将设置更改为 django时。 contrib.sessions.backends.cache 我遇到以下错误:
The request's session was deleted before the request completed
我使用的django版本是django 1.11
我在社区中搜索了类似问题,但找不到解决方法
当我使用设置: django.contrib.sessions.backends.cached_db 时,它可以工作,但是如果我理解正确的话,它将再次使用数据库中的表。
>我得到的另一个错误是:
The request's session was deleted before the request completed.
The user may have logged out in a concurrent request, for example.
(通过反复试验)回答我的问题:
来自django文档https://docs.djangoproject.com/en/2.1/topics/http/sessions/
Set SESSION_ENGINE to "django.contrib.sessions.backends.cache" for a
simple caching session store. Session data will be stored directly in your
cache. However, session data may not be persistent: cached data can be
evicted if the cache fills up or if the cache server is restarted.
For persistent, cached data, set SESSION_ENGINE to
"django.contrib.sessions.backends.cached_db". This uses a write-through
cache – every write to the cache will also be written to the database.
Session reads only use the database if the data is not already in the
cache.
所以我使用的memcached的最终设置是
SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
SESSION_CACHE_ALIAS = "default"
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'TIMEOUT': 120
}
}
答案 0 :(得分:0)
我的猜测是memcached服务未启动,因此您尝试存储在缓存中的会话不会持续存在。您需要确保已启动memcached服务。
sudo apt-get -y install memcached
sudo service memcached restart
希望这会有所帮助。