Skip to content

Instantly share code, notes, and snippets.

@partho222
Forked from aek/RedisSessionStore
Last active July 18, 2022 13:06
Show Gist options
  • Save partho222/ea17521b4f4e5c415bb31c910e619c9a to your computer and use it in GitHub Desktop.
Save partho222/ea17521b4f4e5c415bb31c910e619c9a 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