Skip to content

Instantly share code, notes, and snippets.

@skygunner
Forked from aek/RedisSessionStore
Created February 7, 2023 20:32
Show Gist options
  • Save skygunner/b461e9bf9d7e5c3515a6bc61f8cba107 to your computer and use it in GitHub Desktop.
Save skygunner/b461e9bf9d7e5c3515a6bc61f8cba107 to your computer and use it in GitHub Desktop.
class RedisSessionStore(SessionStore):
def __init__(self, expire = 1800, key_prefix=''):
SessionStore.__init__(self)
self.redis = redis.Redis(tools.config.get('redis_host', 'localhost'),
int(tools.config.get('redis_port', 6379)),
int(tools.config.get('redis_dbindex', 1)),
password=tools.config.get('redis_pass', None))
self.path = session_path()
self.expire = expire
self.key_prefix = key_prefix
def save(self, session):
key = self._get_session_key(session.sid)
data = cPickle.dumps(dict(session))
self.redis.setex(key, data, self.expire)
def delete(self, session):
key = self._get_session_key(session.sid)
self.redis.delete(key)
def _get_session_key(self,sid):
key = self.key_prefix + sid
if isinstance(key, unicode):
key = key.encode('utf-8')
return key
def get(self, sid):
key = self._get_session_key(sid)
data = self.redis.get(key)
if data:
self.redis.setex(key, data, self.expire)
data = cPickle.loads(data)
else:
data = {}
return self.session_class(data, sid, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment