Skip to content

Instantly share code, notes, and snippets.

@aek
Created July 9, 2015 16:24
Show Gist options
  • Save aek/efb0f9dd8935471f9070 to your computer and use it in GitHub Desktop.
Save aek/efb0f9dd8935471f9070 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)
@chowderpally
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment