django-redis-sessions-fork

Redis Session Backend For Django


Keywords
django, sessions, redis
License
BSD-3-Clause
Install
pip install django-redis-sessions-fork==0.7.0

Documentation

django-redis-sessions-fork

info: Redis Session Backend For Django

Features

  • Fast NoSQL Django sessions backend
  • Invalidation via TTL
  • Easy migrations from django.contrib.sessions
  • Fastest session serializers
  • Backward migrations to django.contrib.sessions

Installation

run pip install django-redis-sessions-fork

or alternatively download the tarball and run python setup.py install

set redis_sessions_fork.session as your session engine, like so

SESSION_ENGINE = 'redis_sessions_fork.session'

Configuration

# all these options are defaults, you can skip anyone
SESSION_REDIS_HOST = '127.0.0.1'
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 0
SESSION_REDIS_PASSWORD = None
SESSION_REDIS_PREFIX = None

# 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'

# you can also use redis from url
SESSION_REDIS_URL = 'redis://127.0.0.1:6379/0'

# also available setup connection via redis.ConnectionPool like
SESSION_REDIS_CONNECTION_POOL = 'random.app.redis_connection_pool'

if you one of happy heroku.com users

you can skip redis configuration at all

django-redis-sessions-fork already have prefiguration for redis clouds

Serializer's

Django>=1.5.3 supports different session serializers, such as django.contrib.sessions.serializers.PickleSerializer and django.contrib.sessions.serializers.JSONSerializer

alternative you can use ujson serializer, which is more faster then default

pip install ujson

then

SESSION_SERIALIZER = 'redis_sessions_fork.serializers.UjsonSerializer'

in addition it is possible to configure ujson encoding, like

SESSION_REDIS_JSON_ENCODING = 'utf8' # default is 'latin-1'

Sessions migration

add redis_sessions_fork to your INSTALLED_APPS

# copy orm sessions to redis
python manage.py migrate_sessions_to_redis
# copy redis sessions to orm
python manage.py migrate_sessions_to_orm
# flush redis sessions
python manage.py flush_redis_sessions
# flush orm sessions
python manage.py flush_orm_sessions

Tests

pip install tox
tox