|
@ -2,7 +2,7 @@ |
|
|
# |
|
|
# |
|
|
# Copyright (c) 2017-2019 MuK IT GmbH. |
|
|
# Copyright (c) 2017-2019 MuK IT GmbH. |
|
|
# |
|
|
# |
|
|
# This file is part of MuK Session Store |
|
|
|
|
|
|
|
|
# This file is part of MuK Session Store |
|
|
# (see https://mukit.at). |
|
|
# (see https://mukit.at). |
|
|
# |
|
|
# |
|
|
# This program is free software: you can redistribute it and/or modify |
|
|
# This program is free software: you can redistribute it and/or modify |
|
@ -38,6 +38,7 @@ except ImportError: |
|
|
|
|
|
|
|
|
SESSION_TIMEOUT = 60 * 60 * 24 * 7 |
|
|
SESSION_TIMEOUT = 60 * 60 * 24 * 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def retry_redis(func): |
|
|
def retry_redis(func): |
|
|
@functools.wraps(func) |
|
|
@functools.wraps(func) |
|
|
def wrapper(self, *args, **kwargs): |
|
|
def wrapper(self, *args, **kwargs): |
|
@ -48,10 +49,12 @@ def retry_redis(func): |
|
|
_logger.warn("SessionStore connection failed! (%s/5)" % attempts) |
|
|
_logger.warn("SessionStore connection failed! (%s/5)" % attempts) |
|
|
if attempts >= 5: |
|
|
if attempts >= 5: |
|
|
raise error |
|
|
raise error |
|
|
|
|
|
|
|
|
return wrapper |
|
|
return wrapper |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RedisSessionStore(SessionStore): |
|
|
class RedisSessionStore(SessionStore): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
def __init__(self, *args, **kwargs): |
|
|
super(RedisSessionStore, self).__init__(*args, **kwargs) |
|
|
super(RedisSessionStore, self).__init__(*args, **kwargs) |
|
|
self.prefix = config.get('session_store_prefix', '') |
|
|
self.prefix = config.get('session_store_prefix', '') |
|
@ -59,25 +62,27 @@ class RedisSessionStore(SessionStore): |
|
|
host=config.get('session_store_host', 'localhost'), |
|
|
host=config.get('session_store_host', 'localhost'), |
|
|
port=int(config.get('session_store_port', 6379)), |
|
|
port=int(config.get('session_store_port', 6379)), |
|
|
db=int(config.get('session_store_dbindex', 1)), |
|
|
db=int(config.get('session_store_dbindex', 1)), |
|
|
password=config.get('session_store_pass', None) |
|
|
|
|
|
|
|
|
password=config.get('session_store_pass', None), |
|
|
|
|
|
ssl=config.get("session_store_ssl", False), |
|
|
|
|
|
ssl_cert_reqs=config.get("session_store_ssl_cert_reqs", None), |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _encode_session_key(self, key): |
|
|
def _encode_session_key(self, key): |
|
|
return key.encode('utf-8') if isinstance(key, str) else key |
|
|
return key.encode('utf-8') if isinstance(key, str) else key |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_session_key(self, sid): |
|
|
def _get_session_key(self, sid): |
|
|
return self._encode_session_key(self.prefix + sid) |
|
|
return self._encode_session_key(self.prefix + sid) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@retry_redis |
|
|
@retry_redis |
|
|
def save(self, session): |
|
|
def save(self, session): |
|
|
key = self._get_session_key(session.sid) |
|
|
key = self._get_session_key(session.sid) |
|
|
payload = pickle.dumps(dict(session), pickle.HIGHEST_PROTOCOL) |
|
|
payload = pickle.dumps(dict(session), pickle.HIGHEST_PROTOCOL) |
|
|
self.server.setex(name=key, value=payload, time=SESSION_TIMEOUT) |
|
|
self.server.setex(name=key, value=payload, time=SESSION_TIMEOUT) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@retry_redis |
|
|
@retry_redis |
|
|
def delete(self, session): |
|
|
def delete(self, session): |
|
|
self.server.delete(self._get_session_key(session.sid)) |
|
|
self.server.delete(self._get_session_key(session.sid)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@retry_redis |
|
|
@retry_redis |
|
|
def get(self, sid): |
|
|
def get(self, sid): |
|
|
if not self.is_valid_key(sid): |
|
|
if not self.is_valid_key(sid): |
|
@ -88,4 +93,5 @@ class RedisSessionStore(SessionStore): |
|
|
self.server.setex(name=key, value=payload, time=SESSION_TIMEOUT) |
|
|
self.server.setex(name=key, value=payload, time=SESSION_TIMEOUT) |
|
|
return self.session_class(pickle.loads(payload), sid, False) |
|
|
return self.session_class(pickle.loads(payload), sid, False) |
|
|
else: |
|
|
else: |
|
|
return self.session_class({}, sid, False) |
|
|
|
|
|
|
|
|
return self.session_class({}, sid, False) |
|
|
|
|
|
|