Skip to content

Commit

Permalink
Merge pull request #65 from boltnev/master
Browse files Browse the repository at this point in the history
redis_connection_object connection_type
  • Loading branch information
martinrusev authored Dec 8, 2020
2 parents 1439306 + d93b121 commit 7f875c5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ dist
*.egg
.idea
*.sublime*
*.swp
virtualenv
venv
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nose
django
redis
redis==2.10.3
2 changes: 1 addition & 1 deletion redis_sessions/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.6.1'
__version__ = '0.6.2'
10 changes: 7 additions & 3 deletions redis_sessions/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions redis_sessions/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
SESSION_REDIS_SENTINEL_MASTER_ALIAS = getattr(settings, 'SESSION_REDIS_SENTINEL_MASTER_ALIAS', None)
23 changes: 22 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7f875c5

Please sign in to comment.