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)
@lukebranch
Copy link

@aek,

Nice work!

I'm really interested in storing sessions and cache in Redis for Odoo 8 instances, but i'm not quite sure how to tackle it. Any suggestions or advice you could provide on how you've set it up in your own instances would be really great. Do you have any plans on writing a tutorial for getting this working in Odoo 8?

@chowderpally
Copy link

What's the need to store the sessions in the redis? Is it solving any purpose?
It would be great if someone here can explain the same.

Thanks in advance!

@aek
Copy link
Author

aek commented Jul 22, 2020

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

@chowderpally
Copy link

Thank you!

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