Redis实施后,Django会话停止工作

时间:2016-03-07 11:17:28

标签: python django session redis

我已经使用此link实现了redis,并且在此会话之后必须存储在db的redis instaed中。配置后重新启动我的应用程序。我的会话收到了嘿错误。

如, 让我们说IN函数A我宣布了一个会话,当我从浏览器访问另一个链接并尝试在视图中获取相同的会话时,会话已经消失了。甚至没有会话密钥存在。 request.session.keys()是空白的。

这是我的redis配置:

缓存服务器设置

REDIS_PROT = "redis://"
REDIS_HOST = "127.0.0.1"
REDIS_PORT = 6379
REDIS_DB = 0

REDIS_CACHE_SERVER = REDIS_PROT + REDIS_HOST + ":" + str(REDIS_PORT) + "/" + str(REDIS_DB)
REDIS_CACHEALOT_SERVER = REDIS_PROT + REDIS_HOST + ":" + str(REDIS_PORT) + "/" + str(REDIS_DB + 3)

DJANGO_REDIS_IGNORE_EXCEPTIONS = True

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": REDIS_CACHE_SERVER,
        "OPTIONS": {
            "PARSER_CLASS": "redis.connection.HiredisParser",
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "COMPRESS_MIN_LEN": 10,
            "SOCKET_CONNECT_TIMEOUT": 5,  # in seconds
            "SOCKET_TIMEOUT": 5,  # in seconds
        }
    },
    "cachealot": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": REDIS_CACHEALOT_SERVER,
        "OPTIONS": {
            "PARSER_CLASS": "redis.connection.HiredisParser",
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "COMPRESS_MIN_LEN": 10,
            "SOCKET_CONNECT_TIMEOUT": 5,  # in seconds
            "SOCKET_TIMEOUT": 5,  # in seconds
        }
    }
}

# CacheALot service settings 

CACHALOT_ENABLED = True
CACHALOT_CACHE   = 'cachealot'

# Django Redis Session App Configuration 

SESSION_REDIS_HOST = REDIS_HOST
SESSION_REDIS_PORT = REDIS_PORT
SESSION_REDIS_DB = REDIS_DB + 4
#SESSION_REDIS_PASSWORD = 'password'
SESSION_REDIS_PREFIX = 'session'

SESSION_ENGINE = 'redis_sessions.session'
# If you prefer domain socket connection, you can just add this line instead of SESSION_REDIS_HOST and SESSION_REDIS_PORT.
#SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH = '/var/run/redis/redis.sock'

# Celery Configurations
REDIS_CELERY_BROKER_SERVER =  REDIS_PROT + REDIS_HOST + ":" + str(REDIS_PORT) + "/" + str(REDIS_DB + 1)
REDIS_CELERY_RESULT_SERVER = REDIS_PROT + REDIS_HOST + ":" + str(REDIS_PORT) + "/" + str(REDIS_DB + 2)

BROKER_URL = REDIS_CELERY_BROKER_SERVER
CELERY_RESULT_BACKEND = REDIS_CELERY_RESULT_SERVER
CELERY_ALWAYS_EAGER=False

知道我为什么会收到django会话的关键错误。

1 个答案:

答案 0 :(得分:2)

似乎你为简单地启用缓存会话做了很多工作。除了django-redis-sessions之外,为什么还使用django-cachalotdjango-redis

让我们使用最小配置进行简化,这对我来说只使用django-redis。我关注their guideDjango's documentation on setting up sessionsusing cached sessions

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

通过请求查看并检查request.session或者,如果您重新outside of request, using your session store来测试它:

>>> from django.contrib.sessions.backends.cache import SessionStore
>>> s = SessionStore(session_key='ff882814010ccbc3c870523934fee5a2')
>>> s.save()
>>> s.session_key
'ff882814010ccbc3c870523934fee5a2'
>>> s.session_data
'KGRwMQpTJ19hdXRoX3VzZXJfaWQnCnAyCkkxCnMuMTExY2ZjODI2Yj...'
>>> s.get_decoded()
{'user_id': 42}