Created
July 9, 2015 16:24
-
-
Save aek/efb0f9dd8935471f9070 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
The need that this solve is to allow sharing session storage between multiple Odoo instances in multiple servers. Also with Redis sessions you could get session timeouts