././@PaxHeader0000000000000000000000000000002700000000000011454 xustar000000000000000023 mtime=1610560372.82 django-redis-sessions-0.6.2/0000755000175000017500000000000000000000000016540 5ustar00martinmartin00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/CHANGELOG.md0000644000175000017500000000065200000000000020354 0ustar00martinmartin00000000000000## 0.6.1 (16 September 2017) BUG FIXES: * Dict Config fixes: ([#48](https://github.com/martinrusev/django-redis-sessions/issues/48)) ## 0.6 (6 September 2017) IMPROVEMENTS / BREAKING CHANGES: * Dict config: ([#18](https://github.com/martinrusev/django-redis-sessions/issues/18)) ## 0.5.6 (9 June 2016) IMPROVEMENTS: * Redis Sentinel support: ([#29](https://github.com/martinrusev/django-redis-sessions/pull/29)) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/LICENSE.txt0000644000175000017500000000275700000000000020376 0ustar00martinmartin00000000000000Copyright (c) 2017, Martin Rusev All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/MANIFEST.in0000644000175000017500000000010700000000000020274 0ustar00martinmartin00000000000000include README.rst include LICENSE.txt include CHANGELOG.md graft tests././@PaxHeader0000000000000000000000000000002700000000000011454 xustar000000000000000023 mtime=1610560372.82 django-redis-sessions-0.6.2/PKG-INFO0000644000175000017500000001070100000000000017634 0ustar00martinmartin00000000000000Metadata-Version: 1.1 Name: django-redis-sessions Version: 0.6.2 Summary: Redis Session Backend For Django Home-page: http://github.com/martinrusev/django-redis-sessions Author: Martin Rusev Author-email: martin@amon.cx License: BSD Description: django-redis-sessions ===================== Redis database backend for your sessions |Build Status| - `Installation`_ - `Available Settings`_ - `Changelog`_ Installation ============ - Run ``pip install django-redis-sessions`` or alternatively download the tarball and run ``python setup.py install``, For Django < 1.4 run ``pip install django-redis-sessions==0.3`` - Set ``redis_sessions.session`` as your session engine, like so: .. code:: python SESSION_ENGINE = 'redis_sessions.session' Available Settings ================== .. code:: python SESSION_REDIS = { 'host': 'localhost', 'port': 6379, 'db': 0, 'password': 'password', 'prefix': 'session', 'socket_timeout': 1, 'retry_on_timeout': False } If you prefer domain socket connection, you can just add this line instead of HOST and PORT. .. code:: python SESSION_REDIS = { 'unix_domain_socket_path': '/var/run/redis/redis.sock', 'db': 0, 'password': 'password', 'prefix': 'session', 'socket_timeout': 1, 'retry_on_timeout': False } Redis Sentinel ~~~~~~~~~~~~~~ .. code:: python SESSION_REDIS_SENTINEL_LIST = [(host, port), (host, port), (host, port)] SESSION_REDIS_SENTINEL_MASTER_ALIAS = 'sentinel-master' Redis Pool (Horizontal partitioning) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Splits sessions between Redis instances based on the session key. You can configure the connection type for each Redis instance in the pool (host/port, unix socket, redis url). .. code:: python SESSION_REDIS = { 'prefix': 'session', 'socket_timeout': 1 'retry_on_timeout': False, 'pool': [{ 'host': 'localhost3', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }, { 'host': 'localhost2', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }, { 'host': 'localhost1', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }] } Tests ===== .. code:: bash $ pip install -r dev_requirements.txt # Make sure you have redis running on localhost:6379 $ nosetests -v `Changelog `__ ============================================================================================= .. _Installation: #installation .. _Available Settings: #available-settings .. _Changelog: #changelog .. |Build Status| image:: https://travis-ci.org/martinrusev/django-redis-sessions.svg?branch=master :target: https://travis-ci.org/martinrusev/django-redis-sessions Keywords: django,sessions, Platform: UNKNOWN Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 3 Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Framework :: Django Classifier: Environment :: Web Environment ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/README.rst0000644000175000017500000000571100000000000020233 0ustar00martinmartin00000000000000django-redis-sessions ===================== Redis database backend for your sessions |Build Status| - `Installation`_ - `Available Settings`_ - `Changelog`_ Installation ============ - Run ``pip install django-redis-sessions`` or alternatively download the tarball and run ``python setup.py install``, For Django < 1.4 run ``pip install django-redis-sessions==0.3`` - Set ``redis_sessions.session`` as your session engine, like so: .. code:: python SESSION_ENGINE = 'redis_sessions.session' Available Settings ================== .. code:: python SESSION_REDIS = { 'host': 'localhost', 'port': 6379, 'db': 0, 'password': 'password', 'prefix': 'session', 'socket_timeout': 1, 'retry_on_timeout': False } If you prefer domain socket connection, you can just add this line instead of HOST and PORT. .. code:: python SESSION_REDIS = { 'unix_domain_socket_path': '/var/run/redis/redis.sock', 'db': 0, 'password': 'password', 'prefix': 'session', 'socket_timeout': 1, 'retry_on_timeout': False } Redis Sentinel ~~~~~~~~~~~~~~ .. code:: python SESSION_REDIS_SENTINEL_LIST = [(host, port), (host, port), (host, port)] SESSION_REDIS_SENTINEL_MASTER_ALIAS = 'sentinel-master' Redis Pool (Horizontal partitioning) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Splits sessions between Redis instances based on the session key. You can configure the connection type for each Redis instance in the pool (host/port, unix socket, redis url). .. code:: python SESSION_REDIS = { 'prefix': 'session', 'socket_timeout': 1 'retry_on_timeout': False, 'pool': [{ 'host': 'localhost3', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }, { 'host': 'localhost2', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }, { 'host': 'localhost1', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }] } Tests ===== .. code:: bash $ pip install -r dev_requirements.txt # Make sure you have redis running on localhost:6379 $ nosetests -v `Changelog `__ ============================================================================================= .. _Installation: #installation .. _Available Settings: #available-settings .. _Changelog: #changelog .. |Build Status| image:: https://travis-ci.org/martinrusev/django-redis-sessions.svg?branch=master :target: https://travis-ci.org/martinrusev/django-redis-sessions ././@PaxHeader0000000000000000000000000000002700000000000011454 xustar000000000000000023 mtime=1610560372.82 django-redis-sessions-0.6.2/django_redis_sessions.egg-info/0000755000175000017500000000000000000000000024610 5ustar00martinmartin00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560372.0 django-redis-sessions-0.6.2/django_redis_sessions.egg-info/PKG-INFO0000644000175000017500000001070100000000000025704 0ustar00martinmartin00000000000000Metadata-Version: 1.1 Name: django-redis-sessions Version: 0.6.2 Summary: Redis Session Backend For Django Home-page: http://github.com/martinrusev/django-redis-sessions Author: Martin Rusev Author-email: martin@amon.cx License: BSD Description: django-redis-sessions ===================== Redis database backend for your sessions |Build Status| - `Installation`_ - `Available Settings`_ - `Changelog`_ Installation ============ - Run ``pip install django-redis-sessions`` or alternatively download the tarball and run ``python setup.py install``, For Django < 1.4 run ``pip install django-redis-sessions==0.3`` - Set ``redis_sessions.session`` as your session engine, like so: .. code:: python SESSION_ENGINE = 'redis_sessions.session' Available Settings ================== .. code:: python SESSION_REDIS = { 'host': 'localhost', 'port': 6379, 'db': 0, 'password': 'password', 'prefix': 'session', 'socket_timeout': 1, 'retry_on_timeout': False } If you prefer domain socket connection, you can just add this line instead of HOST and PORT. .. code:: python SESSION_REDIS = { 'unix_domain_socket_path': '/var/run/redis/redis.sock', 'db': 0, 'password': 'password', 'prefix': 'session', 'socket_timeout': 1, 'retry_on_timeout': False } Redis Sentinel ~~~~~~~~~~~~~~ .. code:: python SESSION_REDIS_SENTINEL_LIST = [(host, port), (host, port), (host, port)] SESSION_REDIS_SENTINEL_MASTER_ALIAS = 'sentinel-master' Redis Pool (Horizontal partitioning) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Splits sessions between Redis instances based on the session key. You can configure the connection type for each Redis instance in the pool (host/port, unix socket, redis url). .. code:: python SESSION_REDIS = { 'prefix': 'session', 'socket_timeout': 1 'retry_on_timeout': False, 'pool': [{ 'host': 'localhost3', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }, { 'host': 'localhost2', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }, { 'host': 'localhost1', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1 }] } Tests ===== .. code:: bash $ pip install -r dev_requirements.txt # Make sure you have redis running on localhost:6379 $ nosetests -v `Changelog `__ ============================================================================================= .. _Installation: #installation .. _Available Settings: #available-settings .. _Changelog: #changelog .. |Build Status| image:: https://travis-ci.org/martinrusev/django-redis-sessions.svg?branch=master :target: https://travis-ci.org/martinrusev/django-redis-sessions Keywords: django,sessions, Platform: UNKNOWN Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 3 Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Framework :: Django Classifier: Environment :: Web Environment ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560372.0 django-redis-sessions-0.6.2/django_redis_sessions.egg-info/SOURCES.txt0000644000175000017500000000066500000000000026503 0ustar00martinmartin00000000000000CHANGELOG.md LICENSE.txt MANIFEST.in README.rst setup.py django_redis_sessions.egg-info/PKG-INFO django_redis_sessions.egg-info/SOURCES.txt django_redis_sessions.egg-info/dependency_links.txt django_redis_sessions.egg-info/not-zip-safe django_redis_sessions.egg-info/requires.txt django_redis_sessions.egg-info/top_level.txt redis_sessions/__init__.py redis_sessions/session.py redis_sessions/settings.py tests/__init__.py tests/tests.py././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560372.0 django-redis-sessions-0.6.2/django_redis_sessions.egg-info/dependency_links.txt0000644000175000017500000000000100000000000030656 0ustar00martinmartin00000000000000 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560372.0 django-redis-sessions-0.6.2/django_redis_sessions.egg-info/not-zip-safe0000644000175000017500000000000100000000000027036 0ustar00martinmartin00000000000000 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560372.0 django-redis-sessions-0.6.2/django_redis_sessions.egg-info/requires.txt0000644000175000017500000000001600000000000027205 0ustar00martinmartin00000000000000redis>=2.4.10 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560372.0 django-redis-sessions-0.6.2/django_redis_sessions.egg-info/top_level.txt0000644000175000017500000000001700000000000027340 0ustar00martinmartin00000000000000redis_sessions ././@PaxHeader0000000000000000000000000000002700000000000011454 xustar000000000000000023 mtime=1610560372.82 django-redis-sessions-0.6.2/redis_sessions/0000755000175000017500000000000000000000000021574 5ustar00martinmartin00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/redis_sessions/__init__.py0000644000175000017500000000002600000000000023703 0ustar00martinmartin00000000000000__version__ = '0.6.2' ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/redis_sessions/session.py0000644000175000017500000001535000000000000023635 0ustar00martinmartin00000000000000import redis try: from django.utils.encoding import force_unicode except ImportError: # Python 3.* from django.utils.encoding import force_str as force_unicode from django.contrib.sessions.backends.base import SessionBase, CreateError from redis_sessions import settings class RedisServer(): __redis = {} def __init__(self, session_key): self.session_key = session_key self.connection_key = '' if settings.SESSION_REDIS_SENTINEL_LIST is not None: self.connection_type = 'sentinel' if settings.SESSION_REDIS_CONNECTION_OBJECT is not None: self.connection_type = 'connection_object' else: if settings.SESSION_REDIS_POOL is not None: server_key, server = self.get_server(session_key, settings.SESSION_REDIS_POOL) self.connection_key = str(server_key) settings.SESSION_REDIS_HOST = getattr(server, 'host', 'localhost') settings.SESSION_REDIS_PORT = getattr(server, 'port', 6379) settings.SESSION_REDIS_DB = getattr(server, 'db', 0) settings.SESSION_REDIS_PASSWORD = getattr(server, 'password', None) settings.SESSION_REDIS_URL = getattr(server, 'url', None) settings.SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH = getattr(server,'unix_domain_socket_path', None) if settings.SESSION_REDIS_URL is not None: self.connection_type = 'redis_url' elif settings.SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH is not None: self.connection_type = 'redis_unix_url' elif settings.SESSION_REDIS_HOST is not None: self.connection_type = 'redis_host' self.connection_key += self.connection_type def get_server(self, key, servers_pool): total_weight = sum([row.get('weight', 1) for row in servers_pool]) pos = 0 for i in range(3, -1, -1): pos = pos * 2 ** 8 + ord(key[i]) pos = pos % total_weight pool = iter(servers_pool) server = next(pool) server_key = 0 i = 0 while i < total_weight: if i <= pos < (i + server.get('weight', 1)): return server_key, server i += server.get('weight', 1) server = next(pool) server_key += 1 return def get(self): if self.connection_key in self.__redis: return self.__redis[self.connection_key] if self.connection_type == 'connection_object': self.__redis[self.connection_key] = settings.SESSION_REDIS_CONNECTION_OBJECT elif self.connection_type == 'sentinel': from redis.sentinel import Sentinel self.__redis[self.connection_key] = Sentinel( settings.SESSION_REDIS_SENTINEL_LIST, socket_timeout=settings.SESSION_REDIS_SOCKET_TIMEOUT, retry_on_timeout=settings.SESSION_REDIS_RETRY_ON_TIMEOUT, db=getattr(settings, 'SESSION_REDIS_DB', 0), password=getattr(settings, 'SESSION_REDIS_PASSWORD', None) ).master_for(settings.SESSION_REDIS_SENTINEL_MASTER_ALIAS) elif self.connection_type == 'redis_url': self.__redis[self.connection_key] = redis.StrictRedis.from_url( settings.SESSION_REDIS_URL, socket_timeout=settings.SESSION_REDIS_SOCKET_TIMEOUT ) elif self.connection_type == 'redis_host': self.__redis[self.connection_key] = redis.StrictRedis( host=settings.SESSION_REDIS_HOST, port=settings.SESSION_REDIS_PORT, socket_timeout=settings.SESSION_REDIS_SOCKET_TIMEOUT, retry_on_timeout=settings.SESSION_REDIS_RETRY_ON_TIMEOUT, db=settings.SESSION_REDIS_DB, password=settings.SESSION_REDIS_PASSWORD ) elif self.connection_type == 'redis_unix_url': self.__redis[self.connection_key] = redis.StrictRedis( unix_socket_path=settings.SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH, socket_timeout=settings.SESSION_REDIS_SOCKET_TIMEOUT, retry_on_timeout=settings.SESSION_REDIS_RETRY_ON_TIMEOUT, db=settings.SESSION_REDIS_DB, password=settings.SESSION_REDIS_PASSWORD, ) return self.__redis[self.connection_key] class SessionStore(SessionBase): """ Implements Redis database session store. """ def __init__(self, session_key=None): super(SessionStore, self).__init__(session_key) self.server = RedisServer(session_key).get() def load(self): try: session_data = self.server.get( self.get_real_stored_key(self._get_or_create_session_key()) ) return self.decode(force_unicode(session_data)) except: self._session_key = None return {} def exists(self, session_key): return self.server.exists(self.get_real_stored_key(session_key)) def create(self): while True: self._session_key = self._get_new_session_key() try: self.save(must_create=True) except CreateError: # Key wasn't unique. Try again. continue self.modified = True return def save(self, must_create=False): if self.session_key is None: return self.create() if must_create and self.exists(self._get_or_create_session_key()): raise CreateError data = self.encode(self._get_session(no_load=must_create)) if redis.VERSION[0] >= 2: self.server.setex( self.get_real_stored_key(self._get_or_create_session_key()), self.get_expiry_age(), data ) else: self.server.set( self.get_real_stored_key(self._get_or_create_session_key()), data ) self.server.expire( self.get_real_stored_key(self._get_or_create_session_key()), self.get_expiry_age() ) def delete(self, session_key=None): if session_key is None: if self.session_key is None: return session_key = self.session_key try: self.server.delete(self.get_real_stored_key(session_key)) except: pass @classmethod def clear_expired(cls): pass def get_real_stored_key(self, session_key): """Return the real key name in redis storage @return string """ prefix = settings.SESSION_REDIS_PREFIX if not prefix: return session_key return ':'.join([prefix, session_key]) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/redis_sessions/settings.py0000644000175000017500000000274100000000000024012 0ustar00martinmartin00000000000000from django.conf import settings # SESSION_REDIS - Default SESSION_REDIS = getattr(settings, 'SESSION_REDIS', {}) SESSION_REDIS_CONNECTION_OBJECT = getattr(settings, 'SESSION_REDIS_CONNECTION_OBJECT', None) SESSION_REDIS_HOST = SESSION_REDIS.get('host', 'localhost') SESSION_REDIS_PORT = SESSION_REDIS.get('port', 6379) SESSION_REDIS_SOCKET_TIMEOUT = SESSION_REDIS.get('socket_timeout', 0.1) SESSION_REDIS_RETRY_ON_TIMEOUT = SESSION_REDIS.get('retry_on_timeout', False) SESSION_REDIS_DB = SESSION_REDIS.get('db', 0) SESSION_REDIS_PREFIX = SESSION_REDIS.get('prefix', '') SESSION_REDIS_PASSWORD = SESSION_REDIS.get('password', None) SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH = SESSION_REDIS.get('unix_domain_socket_path', None) SESSION_REDIS_URL = SESSION_REDIS.get('url', None) """ Should be on the format: [ { 'host': 'localhost2', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1, }, { 'host': 'localhost1', 'port': 6379, 'db': 0, 'password': None, 'unix_domain_socket_path': None, 'url': None, 'weight': 1, }, ] """ SESSION_REDIS_POOL = SESSION_REDIS.get('POOL', None) # should be on the format [(host, port), (host, port), (host, port)] SESSION_REDIS_SENTINEL_LIST = getattr(settings, 'SESSION_REDIS_SENTINEL_LIST', None) SESSION_REDIS_SENTINEL_MASTER_ALIAS = getattr(settings, 'SESSION_REDIS_SENTINEL_MASTER_ALIAS', None) ././@PaxHeader0000000000000000000000000000002700000000000011454 xustar000000000000000023 mtime=1610560372.82 django-redis-sessions-0.6.2/setup.cfg0000644000175000017500000000004600000000000020361 0ustar00martinmartin00000000000000[egg_info] tag_build = tag_date = 0 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/setup.py0000644000175000017500000000166300000000000020260 0ustar00martinmartin00000000000000from setuptools import setup import os from redis_sessions import __version__ def read(filename): return open(os.path.join(os.path.dirname(__file__), filename)).read() packages = ['redis_sessions'] setup( name='django-redis-sessions', version=__version__, description= "Redis Session Backend For Django", long_description=read("README.rst"), keywords='django, sessions,', author='Martin Rusev', author_email='martin@amon.cx', url='http://github.com/martinrusev/django-redis-sessions', license='BSD', packages=packages, zip_safe=False, install_requires=['redis>=2.4.10'], include_package_data=True, classifiers=[ "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Software Development :: Libraries :: Python Modules", "Framework :: Django", "Environment :: Web Environment", ], test_suite='tests' ) ././@PaxHeader0000000000000000000000000000002700000000000011454 xustar000000000000000023 mtime=1610560372.82 django-redis-sessions-0.6.2/tests/0000755000175000017500000000000000000000000017702 5ustar00martinmartin00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/tests/__init__.py0000644000175000017500000000014500000000000022013 0ustar00martinmartin00000000000000from django.conf import settings settings.configure( SESSION_ENGINE='redis_sessions.session' ) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1610560315.0 django-redis-sessions-0.6.2/tests/tests.py0000644000175000017500000001344700000000000021427 0ustar00martinmartin00000000000000import time from random import randint from nose.tools import eq_, assert_false from redis_sessions.session import SessionStore from redis_sessions.session import RedisServer from redis_sessions import settings ## Dev import redis import timeit redis_session = SessionStore() def test_modify_and_keys(): eq_(redis_session.modified, False) redis_session['test'] = 'test_me' eq_(redis_session.modified, True) eq_(redis_session['test'], 'test_me') def test_session_load_does_not_create_record(): session = SessionStore('someunknownkey') session.load() eq_(redis_session.exists(redis_session.session_key), False) def test_save_and_delete(): redis_session['key'] = 'value' redis_session.save() eq_(redis_session.exists(redis_session.session_key), True) redis_session.delete(redis_session.session_key) eq_(redis_session.exists(redis_session.session_key), False) def test_flush(): redis_session['key'] = 'another_value' redis_session.save() key = redis_session.session_key redis_session.flush() eq_(redis_session.exists(key), False) def test_items(): redis_session['item1'], redis_session['item2'] = 1, 2 redis_session.save() # Python 3.* eq_(set(list(redis_session.items())), set([('item2', 2), ('item1', 1)])) def test_expiry(): redis_session.set_expiry(1) # Test if the expiry age is set correctly eq_(redis_session.get_expiry_age(), 1) redis_session['key'] = 'expiring_value' redis_session.save() key = redis_session.session_key eq_(redis_session.exists(key), True) time.sleep(2) eq_(redis_session.exists(key), False) def test_save_and_load(): redis_session.set_expiry(60) redis_session.setdefault('item_test', 8) redis_session.save() session_data = redis_session.load() eq_(session_data.get('item_test'), 8) def test_with_redis_url_config(): settings.SESSION_REDIS_URL = 'redis://localhost' from redis_sessions.session import SessionStore redis_session = SessionStore() server = redis_session.server host = server.connection_pool.connection_kwargs.get('host') port = server.connection_pool.connection_kwargs.get('port') db = server.connection_pool.connection_kwargs.get('db') eq_(host, 'localhost') eq_(port, 6379) eq_(db, 0) def test_one_connection_is_used(): session = SessionStore('session_key_1') session['key1'] = 'value1' session.save() redis_server = session.server set_client_name_1 = 'client_name_' + str(randint(1, 1000)) redis_server.client_setname(set_client_name_1) client_name_1 = redis_server.client_getname() eq_(set_client_name_1, client_name_1) del session session = SessionStore('session_key_2') session['key2'] = 'value2' session.save() redis_server = session.server client_name_2 = redis_server.client_getname() eq_(client_name_1, client_name_2) def test_redis_pool_server_select(): servers = [ { 'HOST': 'localhost2', 'PORT': 6379, 'DB': 0, 'PASSWORD': None, 'UNIX_DOMAIN_SOCKET_PATH': None, 'WEIGHT': 1, }, { 'HOST': 'localhost1', 'PORT': 6379, 'DB': 0, 'PASSWORD': None, 'UNIX_DOMAIN_SOCKET_PATH': None, 'WEIGHT': 1, }, ] keys1 = [ 'm8f0os91g40fsq8eul6tejqpp6k22', 'kcffsbb5o272et1d5e6ib7gh75pd9', 'gqldpha87m8183vl9s8uqobcr2ws3', 'ukb9bg2jifrr60fstla67knjv3e32', 'k3dranjfna7fv7ijpofs6l6bj2pw1', 'an4no833idr9jddr960r8ikai5nh3', '16b9gardpcscrj5q4a4kf3c4u7tq8', 'etdefnorfbvfc165c5airu77p2pl9', 'mr778ou0sqqme21gjdiu4drtc0bv4', 'ctkgd8knu5hukdrdue6im28p90kt7' ] keys2 = [ 'jgpsbmjj6030fdr3aefg37nq47nb8', 'prsv0trk66jc100pipm6bb78c3pl2', '84ksqj2vqral7c6ped9hcnq940qq1', 'bv2uc3q48rm8ubipjmolgnhul0ou3', '6c8oph72pfsg3db37qsefn3746fg4', 'tbc0sjtl2bkp5i9n2j2jiqf4r0bg9', 'v0on9rorn71913o3rpqhvkknc1wm5', 'lmsv98ns819uo2klk3s1nusqm0mr0', '0foo2bkgvrlk3jt2tjbssrsc47tr3', '05ure0f6r5jjlsgaimsuk4n1k2sx6', ] rs = RedisServer('') for key in keys1: server_key, server = rs.get_server(key, servers) eq_(server_key, 1) for key in keys2: server_key, server = rs.get_server(key, servers) eq_(server_key, 0) def test_redis_connection_object(): settings.SESSION_REDIS_CONNECTION_OBJECT = redis.StrictRedis( host='redis', port=1234, db=12, ) from redis_sessions.session import SessionStore redis_session = SessionStore() server = redis_session.server host = server.connection_pool.connection_kwargs.get('host') port = server.connection_pool.connection_kwargs.get('port') db = server.connection_pool.connection_kwargs.get('db') eq_(host, 'redis') eq_(port, 1234) eq_(db, 12) def test_with_unix_url_config(): pass # Uncomment this in `redis.conf`: # # unixsocket /tmp/redis.sock # unixsocketperm 755 #settings.SESSION_REDIS_URL = 'unix:///tmp/redis.sock' #from redis_sessions.session import SessionStore # redis_session = SessionStore() # server = redis_session.server # # host = server.connection_pool.connection_kwargs.get('host') # port = server.connection_pool.connection_kwargs.get('port') # db = server.connection_pool.connection_kwargs.get('db') # # eq_(host, 'localhost') # eq_(port, 6379) # eq_(db, 0) # def test_load(): # redis_session.set_expiry(60) # redis_session['item1'], redis_session['item2'] = 1,2 # redis_session.save() # session_data = redis_session.server.get(redis_session.session_key) # expiry, data = int(session_data[:15]), session_data[15:]