简体   繁体   English

Django 中 Memcached 上的会话 - Memcached 中没有项目

[英]Sessions on Memcached in Django - no items in Memcached

I'm setting up sessions in Django using memcached, and after logging in, no items appear in the cache.我正在使用 memcached 在 Django 中设置会话,登录后,缓存中没有出现任何项目。

I can connect to my memcached instance with telnet localhost 11211 and stats says the process is running.我可以使用telnet localhost 11211连接到我的 memcached 实例,并且stats显示该进程正在运行。 My cache settings are as follows:我的缓存设置如下:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': '127.0.0.1:11211' # can also be a list of locations
    }
}
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'

and I do have the database set up for the caching.我确实为缓存设置了数据库。 The sessions show up in the database.会话显示在数据库中。 (when using just backends.cache nothing shows up in the database or the cache.) (当仅使用backends.cache时,数据库或缓存中不会显示任何内容。)

So is this the expected behavior?这是预期的行为吗? Do I have to store something special in the session key for it to register?我是否必须在 session 密钥中存储一些特殊的东西才能注册?

Use django.core.cache.backends.locmem.LocMemCache as below使用django.core.cache.backends.locmem.LocMemCache如下

CACHES={
        "default":{
                    "BACKEND":"django.core.cache.backends.memcached.MemcachedCache",
                    "LOCATION": "127.0.0.1:11211"
                    },

        }

I'm using 1.2 and was reading the docs for 1.3.我正在使用 1.2 并且正在阅读 1.3 的文档。

In 1.2, the cache looks like this: CACHE_BACKEND = "memcached://127.0.0.1:11211/" instead of the caches dictionary.在 1.2 中,缓存看起来像这样: CACHE_BACKEND = "memcached://127.0.0.1:11211/"而不是缓存字典。

You also need to add two middleware classes into your MIDDLEWARE_CLASSES setting as described here: https://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs#the-per-site-cache您还需要将两个中间件类添加到您的 MIDDLEWARE_CLASSES 设置中,如下所述: https://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs#the-per-site-cache

MIDDLEWARE_CLASSES = ( 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware', ) MIDDLEWARE_CLASSES = ('django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware', )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM