diff --git a/.gitignore b/.gitignore index 3254a0f..0fb789a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ dist *.egg .idea *.sublime* +*.swp +virtualenv +venv diff --git a/.travis.yml b/.travis.yml index 1b9bf65..f0b41f5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,5 @@ matrix: - python: "pypy" env: DJANGO="django>=1.4,<2.0" install: - - pip install $DJANGO - - pip install -q redis + - pip install -r <(cat dev_requirements.txt | sed "s/django/$DJANGO/") script: python setup.py nosetests diff --git a/dev_requirements.txt b/dev_requirements.txt index dbbfb63..147f510 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1,3 +1,3 @@ nose django -redis \ No newline at end of file +redis==2.10.3 diff --git a/redis_sessions/__init__.py b/redis_sessions/__init__.py index 8411e55..aece342 100644 --- a/redis_sessions/__init__.py +++ b/redis_sessions/__init__.py @@ -1 +1 @@ -__version__ = '0.6.1' +__version__ = '0.6.2' diff --git a/redis_sessions/session.py b/redis_sessions/session.py index 35e7025..57972a8 100644 --- a/redis_sessions/session.py +++ b/redis_sessions/session.py @@ -17,6 +17,8 @@ def __init__(self, session_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) @@ -34,7 +36,7 @@ def __init__(self, session_key): 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): @@ -61,7 +63,9 @@ def get(self): if self.connection_key in self.__redis: return self.__redis[self.connection_key] - if self.connection_type == 'sentinel': + 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, @@ -165,7 +169,7 @@ def delete(self, session_key=None): @classmethod def clear_expired(cls): pass - + def get_real_stored_key(self, session_key): """Return the real key name in redis storage @return string diff --git a/redis_sessions/settings.py b/redis_sessions/settings.py index 6b89378..c7797d8 100644 --- a/redis_sessions/settings.py +++ b/redis_sessions/settings.py @@ -2,7 +2,7 @@ # 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) @@ -41,4 +41,4 @@ # 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_MASTER_ALIAS = getattr(settings, 'SESSION_REDIS_SENTINEL_MASTER_ALIAS', None) diff --git a/tests/tests.py b/tests/tests.py index fbef31d..253b4fa 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -167,11 +167,32 @@ def test_redis_pool_server_select(): 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