[Python-modules-commits] [django-redis-sessions] 01/05: Imported Upstream version 0.6.1
Scott Kitterman
kitterman at moszumanska.debian.org
Mon Jan 8 20:38:35 UTC 2018
This is an automated email from the git hooks/post-receive script.
kitterman pushed a commit to branch debian/master
in repository django-redis-sessions.
commit e436cfd2aa77dd2b20bf5492aff130082f59ed2e
Author: Scott Kitterman <scott at kitterman.com>
Date: Mon Jan 8 15:34:21 2018 -0500
Imported Upstream version 0.6.1
---
CHANGELOG.md | 21 +++
LICENSE.txt | 2 +-
MANIFEST.in | 4 +-
PKG-INFO | 150 ++++++++++++++++------
README.md | 56 --------
README.rst | 122 ++++++++++++++++++
django_redis_sessions.egg-info/PKG-INFO | 150 ++++++++++++++++------
django_redis_sessions.egg-info/SOURCES.txt | 12 +-
django_redis_sessions.egg-info/pbr.json | 1 -
redis_sessions/__init__.py | 2 +-
redis_sessions/session.py | 128 +++++++++++++------
redis_sessions/settings.py | 56 +++++---
setup.py | 3 +-
tests/__init__.py | 6 +
tests/__init__.pyc | Bin 0 -> 288 bytes
tests/__pycache__/__init__.cpython-35.pyc | Bin 0 -> 263 bytes
tests/__pycache__/tests.cpython-35.pyc | Bin 0 -> 4678 bytes
tests/tests.py | 198 +++++++++++++++++++++++++++++
tests/tests.pyc | Bin 0 -> 5193 bytes
19 files changed, 706 insertions(+), 205 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..efaec90
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,21 @@
+## 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:
+
+ * 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))
\ No newline at end of file
diff --git a/LICENSE.txt b/LICENSE.txt
index f07b69a..32c95d6 100644
--- a/LICENSE.txt
+++ b/LICENSE.txt
@@ -1,4 +1,4 @@
-Copyright (c) 2010, Martin Rusev
+Copyright (c) 2017, Martin Rusev
All rights reserved.
Redistribution and use in source and binary forms, with or without
diff --git a/MANIFEST.in b/MANIFEST.in
index 3e677d0..13cb28c 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,2 +1,4 @@
-include README.md
+include README.rst
include LICENSE.txt
+include CHANGELOG.md
+graft tests
\ No newline at end of file
diff --git a/PKG-INFO b/PKG-INFO
index 39af6c5..970cf7f 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,67 +1,133 @@
Metadata-Version: 1.1
Name: django-redis-sessions
-Version: 0.5.6
+Version: 0.6.1
Summary: Redis Session Backend For Django
Home-page: http://github.com/martinrusev/django-redis-sessions
Author: Martin Rusev
Author-email: martin at amon.cx
License: BSD
Description: django-redis-sessions
- =======================
- Redis database backend for your sessions
+ =====================
+ Redis database backend for your sessions
- [](https://travis-ci.org/martinrusev/django-redis-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:
-
-
- ```
- SESSION_ENGINE = 'redis_sessions.session'
- ```
-
- * Optional settings:
+ - 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
+ }
+
+ 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
+ }
+
+ 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
+ }]
+ }
- ```
- SESSION_REDIS_HOST = 'localhost'
- SESSION_REDIS_PORT = 6379
- SESSION_REDIS_DB = 0
- SESSION_REDIS_PASSWORD = 'password'
- SESSION_REDIS_PREFIX = 'session'
- SESSION_REDIS_SOCKET_TIMEOUT=1
-
- # 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'
-
- # Redis Sentinel
- SESSION_REDIS_SENTINEL_LIST = [(host, port), (host, port), (host, port)]
- SESSION_REDIS_SENTINEL_MASTER_ALIAS = 'sentinel-master'
-
- ```
+ Tests
+ =====
+ .. code:: bash
- Tests
- ============
+ $ pip install -r dev_requirements.txt
+ # Make sure you have redis running on localhost:6379
+ $ nosetests -v
+ `Changelog <https://github.com/martinrusev/django-redis-sessions/blob/master/CHANGELOG.md>`__
+ =============================================================================================
- ```
- $ pip install django nose redis
- # Make sure you have redis running on localhost:6379
- $ nosetests
- ```
+ .. _Installation: #installation
+ .. _Available Settings: #available-settings
+ .. _Changelog: #changelog
- # [Changelog](https://github.com/martinrusev/imbox/blob/master/CHANGELOG.md)
+ .. |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
diff --git a/README.md b/README.md
deleted file mode 100644
index b6a6d2a..0000000
--- a/README.md
+++ /dev/null
@@ -1,56 +0,0 @@
-django-redis-sessions
-=======================
-Redis database backend for your sessions
-
-
-[](https://travis-ci.org/martinrusev/django-redis-sessions)
-
-
-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:
-
-
-```
-SESSION_ENGINE = 'redis_sessions.session'
-```
-
-* Optional settings:
-
-```
-SESSION_REDIS_HOST = 'localhost'
-SESSION_REDIS_PORT = 6379
-SESSION_REDIS_DB = 0
-SESSION_REDIS_PASSWORD = 'password'
-SESSION_REDIS_PREFIX = 'session'
-SESSION_REDIS_SOCKET_TIMEOUT=1
-
-# 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'
-
-# Redis Sentinel
-SESSION_REDIS_SENTINEL_LIST = [(host, port), (host, port), (host, port)]
-SESSION_REDIS_SENTINEL_MASTER_ALIAS = 'sentinel-master'
-
-```
-
-
-
-Tests
-============
-
-
-```
-$ pip install django nose redis
-# Make sure you have redis running on localhost:6379
-$ nosetests
-```
-
-# [Changelog](https://github.com/martinrusev/imbox/blob/master/CHANGELOG.md)
\ No newline at end of file
diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..1d44f32
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,122 @@
+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
+ }
+
+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
+ }
+
+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 <https://github.com/martinrusev/django-redis-sessions/blob/master/CHANGELOG.md>`__
+=============================================================================================
+
+.. _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
\ No newline at end of file
diff --git a/django_redis_sessions.egg-info/PKG-INFO b/django_redis_sessions.egg-info/PKG-INFO
index 39af6c5..970cf7f 100644
--- a/django_redis_sessions.egg-info/PKG-INFO
+++ b/django_redis_sessions.egg-info/PKG-INFO
@@ -1,67 +1,133 @@
Metadata-Version: 1.1
Name: django-redis-sessions
-Version: 0.5.6
+Version: 0.6.1
Summary: Redis Session Backend For Django
Home-page: http://github.com/martinrusev/django-redis-sessions
Author: Martin Rusev
Author-email: martin at amon.cx
License: BSD
Description: django-redis-sessions
- =======================
- Redis database backend for your sessions
+ =====================
+ Redis database backend for your sessions
- [](https://travis-ci.org/martinrusev/django-redis-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:
-
-
- ```
- SESSION_ENGINE = 'redis_sessions.session'
- ```
-
- * Optional settings:
+ - 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
+ }
+
+ 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
+ }
+
+ 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
+ }]
+ }
- ```
- SESSION_REDIS_HOST = 'localhost'
- SESSION_REDIS_PORT = 6379
- SESSION_REDIS_DB = 0
- SESSION_REDIS_PASSWORD = 'password'
- SESSION_REDIS_PREFIX = 'session'
- SESSION_REDIS_SOCKET_TIMEOUT=1
-
- # 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'
-
- # Redis Sentinel
- SESSION_REDIS_SENTINEL_LIST = [(host, port), (host, port), (host, port)]
- SESSION_REDIS_SENTINEL_MASTER_ALIAS = 'sentinel-master'
-
- ```
+ Tests
+ =====
+ .. code:: bash
- Tests
- ============
+ $ pip install -r dev_requirements.txt
+ # Make sure you have redis running on localhost:6379
+ $ nosetests -v
+ `Changelog <https://github.com/martinrusev/django-redis-sessions/blob/master/CHANGELOG.md>`__
+ =============================================================================================
- ```
- $ pip install django nose redis
- # Make sure you have redis running on localhost:6379
- $ nosetests
- ```
+ .. _Installation: #installation
+ .. _Available Settings: #available-settings
+ .. _Changelog: #changelog
- # [Changelog](https://github.com/martinrusev/imbox/blob/master/CHANGELOG.md)
+ .. |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
diff --git a/django_redis_sessions.egg-info/SOURCES.txt b/django_redis_sessions.egg-info/SOURCES.txt
index daa21bc..eecb21c 100644
--- a/django_redis_sessions.egg-info/SOURCES.txt
+++ b/django_redis_sessions.egg-info/SOURCES.txt
@@ -1,14 +1,20 @@
+CHANGELOG.md
LICENSE.txt
MANIFEST.in
-README.md
+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/pbr.json
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
\ No newline at end of file
+redis_sessions/settings.py
+tests/__init__.py
+tests/__init__.pyc
+tests/tests.py
+tests/tests.pyc
+tests/__pycache__/__init__.cpython-35.pyc
+tests/__pycache__/tests.cpython-35.pyc
\ No newline at end of file
diff --git a/django_redis_sessions.egg-info/pbr.json b/django_redis_sessions.egg-info/pbr.json
deleted file mode 100644
index 1a535c5..0000000
--- a/django_redis_sessions.egg-info/pbr.json
+++ /dev/null
@@ -1 +0,0 @@
-{"is_release": false, "git_version": "7be7b21"}
\ No newline at end of file
diff --git a/redis_sessions/__init__.py b/redis_sessions/__init__.py
index 8701e4d..8411e55 100644
--- a/redis_sessions/__init__.py
+++ b/redis_sessions/__init__.py
@@ -1 +1 @@
-__version__ = '0.5.6'
+__version__ = '0.6.1'
diff --git a/redis_sessions/session.py b/redis_sessions/session.py
index 4841a7e..93b75f6 100644
--- a/redis_sessions/session.py
+++ b/redis_sessions/session.py
@@ -8,44 +8,93 @@ from django.contrib.sessions.backends.base import SessionBase, CreateError
from redis_sessions import settings
-# Avoid new redis connection on each request
-
-if settings.SESSION_REDIS_SENTINEL_LIST is not None:
- from redis.sentinel import Sentinel
-
- redis_server = 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 settings.SESSION_REDIS_URL is not None:
-
- redis_server = redis.StrictRedis.from_url(
- settings.SESSION_REDIS_URL,
- socket_timeout=settings.SESSION_REDIS_SOCKET_TIMEOUT
- )
-elif settings.SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH is None:
-
- redis_server = 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
- )
-else:
-
- redis_server = 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,
- )
+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'
+ 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_HOST is not None:
+ self.connection_type = 'redis_host'
+ elif settings.SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH is not None:
+ self.connection_type = 'redis_unix_url'
+
+ 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 == '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, 'db', 0),
+ password=getattr(settings, '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):
@@ -54,8 +103,7 @@ class SessionStore(SessionBase):
"""
def __init__(self, session_key=None):
super(SessionStore, self).__init__(session_key)
-
- self.server = redis_server
+ self.server = RedisServer(session_key).get()
def load(self):
try:
diff --git a/redis_sessions/settings.py b/redis_sessions/settings.py
index 5f49ae9..6b89378 100644
--- a/redis_sessions/settings.py
+++ b/redis_sessions/settings.py
@@ -1,22 +1,44 @@
from django.conf import settings
+# SESSION_REDIS - Default
+SESSION_REDIS = getattr(settings, 'SESSION_REDIS', {})
-SESSION_REDIS_HOST = getattr(settings, 'SESSION_REDIS_HOST', 'localhost')
-SESSION_REDIS_PORT = getattr(settings, 'SESSION_REDIS_PORT', 6379)
-SESSION_REDIS_SOCKET_TIMEOUT = getattr(settings, 'SESSION_REDIS_SOCKET_TIMEOUT', 0.1)
-SESSION_REDIS_RETRY_ON_TIMEOUT = getattr(settings, 'SESSION_REDIS_RETRY_ON_TIMEOUT', False)
-SESSION_REDIS_DB = getattr(settings, 'SESSION_REDIS_DB', 0)
-SESSION_REDIS_PREFIX = getattr(settings, 'SESSION_REDIS_PREFIX', '')
-SESSION_REDIS_PASSWORD = getattr(settings, 'SESSION_REDIS_PASSWORD', None)
-SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH = getattr(
- settings, 'SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH', None
-)
-SESSION_REDIS_URL = getattr(settings, 'SESSION_REDIS_URL', 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
-)
\ No newline at end of file
+SESSION_REDIS_SENTINEL_LIST = getattr(settings, 'SESSION_REDIS_SENTINEL_LIST', None)
+SESSION_REDIS_SENTINEL_MASTER_ALIAS = getattr(settings, 'SESSION_REDIS_SENTINEL_MASTER_ALIAS', None)
\ No newline at end of file
diff --git a/setup.py b/setup.py
index b212ec3..08ad7d5 100644
--- a/setup.py
+++ b/setup.py
@@ -12,7 +12,7 @@ setup(
name='django-redis-sessions',
version=__version__,
description= "Redis Session Backend For Django",
- long_description=read("README.md"),
+ long_description=read("README.rst"),
keywords='django, sessions,',
author='Martin Rusev',
author_email='martin at amon.cx',
@@ -29,4 +29,5 @@ setup(
"Framework :: Django",
"Environment :: Web Environment",
],
+ test_suite='tests'
)
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..f1bcde4
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1,6 @@
+from django.conf import settings
+
+
+settings.configure(
+ SESSION_ENGINE='redis_sessions.session'
+)
diff --git a/tests/__init__.pyc b/tests/__init__.pyc
new file mode 100644
index 0000000..6df0fe1
Binary files /dev/null and b/tests/__init__.pyc differ
diff --git a/tests/__pycache__/__init__.cpython-35.pyc b/tests/__pycache__/__init__.cpython-35.pyc
new file mode 100644
index 0000000..bc4a7cc
Binary files /dev/null and b/tests/__pycache__/__init__.cpython-35.pyc differ
diff --git a/tests/__pycache__/tests.cpython-35.pyc b/tests/__pycache__/tests.cpython-35.pyc
new file mode 100644
index 0000000..f498278
Binary files /dev/null and b/tests/__pycache__/tests.cpython-35.pyc differ
diff --git a/tests/tests.py b/tests/tests.py
new file mode 100644
index 0000000..fbef31d
--- /dev/null
+++ b/tests/tests.py
@@ -0,0 +1,198 @@
+import 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)
... 138 lines suppressed ...
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/django-redis-sessions.git
More information about the Python-modules-commits
mailing list