Sunday, December 8, 2013

Django Redis backend for sessions on Heroku

Session queries take significant time at each view request. To bypass PostgreSQL and obtain the session information, you can use Redis.

On Heroku, add RedisCloud addon to your app. Then install django-redis-sessions and  add the following lines to your production settings file.

########## REDIS SESSION ###########
from os import environ

redis_url = environ.get("REDISCLOUD_URL")
credential_part, host_part = redis_url.split("//")[1].split("@")
rediscloud, rpassword = credential_part.split(":")
rhost, rport = host_part.split(":")

SESSION_REDIS_PASSWORD = rpassword
SESSION_REDIS_HOST = rhost
SESSION_REDIS_PORT = int(rport)
SESSION_ENGINE = 'redis_sessions.session'
SESSION_REDIS_DB = 0
SESSION_REDIS_PREFIX = 'session'
######## END REDIS SESSION #########

No comments: