Reputation: 1130
I Havve implemented redis using this link and after this session must bne stored in redis instaed of db. After configuration when I restart my app . I am getting hey error for my sessions.
e.g.,
Let say IN function A
I declared a session and when I visit another link from browser and try to fetch same session in view the session is gone its not there. even no session keys exist . request.session.keys() is blank.
Here is my redis configuration:
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
Any idea why I am getting key errors for django sessions.
Upvotes: 2
Views: 1660
Reputation: 10840
Seems you're doing awfully much for simply enabling cached sessions. Why are you also using django-redis-sessions, django-cachalot in addition to django-redis?
Let's simplify with a minimal config, that works for me, using only django-redis. I'm following their guide, and Django's documentation on setting up sessions and using 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'
That's it, test it by requesting a view and inspecting request.session
or, if you're 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}
Upvotes: 3