1 # Copyright (C) 2014 Ipsilon project Contributors, for licensee see COPYING
4 from cherrypy.lib.sessions import Session
5 from ipsilon.util.data import SqlStore, SqlQuery
8 import cPickle as pickle
13 SESSION_COLUMNS = ['id', 'data', 'expiration_time']
16 class SqlSession(Session):
24 def setup(cls, **kwargs):
25 """Initialization from cherrypy"""
27 for k, v in kwargs.items():
28 if k == 'storage_dburi':
31 cls._db = SqlStore(cls.dburi)
34 q = SqlQuery(self._db, 'sessions', SESSION_COLUMNS)
35 result = q.select({'id': self.id})
36 return True if result.fetchone() else False
39 q = SqlQuery(self._db, 'sessions', SESSION_COLUMNS)
40 result = q.select({'id': self.id})
43 data = str(base64.b64decode(r[1]))
44 return pickle.loads(data)
46 def _save(self, expiration_time):
49 q = SqlQuery(self._db, 'sessions', SESSION_COLUMNS, trans=True)
50 q.delete({'id': self.id})
51 data = pickle.dumps((self._data, expiration_time), self._proto)
52 q.insert((self.id, base64.b64encode(data), expiration_time))
54 except Exception: # pylint: disable=broad-except
60 q = SqlQuery(self._db, 'sessions', SESSION_COLUMNS)
61 q.delete({'id': self.id})
63 # copy what RamSession does for now
64 def acquire_lock(self):
65 """Acquire an exclusive lock on the currently-loaded session data."""
67 self.locks.setdefault(self.id, threading.RLock()).acquire()
69 def release_lock(self):
70 """Release the lock on the currently-loaded session data."""
71 self.locks[self.id].release()