3 # Copyright (C) 2014 Ipsilon project Contributors, for licensee see COPYING
6 from cherrypy.lib.sessions import Session
7 from ipsilon.util.data import SqlStore, SqlQuery
10 import cPickle as pickle
15 SESSION_COLUMNS = ['id', 'data', 'expiration_time']
18 class SqlSession(Session):
26 def setup(cls, **kwargs):
27 """Initialization from cherrypy"""
29 for k, v in kwargs.items():
30 if k == 'storage_dburi':
33 cls._db = SqlStore(cls.dburi)
36 q = SqlQuery(self._db, 'sessions', SESSION_COLUMNS)
37 result = q.select({'id': self.id})
38 return True if result.fetchone() else False
41 q = SqlQuery(self._db, 'sessions', SESSION_COLUMNS)
42 result = q.select({'id': self.id})
45 data = str(base64.b64decode(r[1]))
46 return pickle.loads(data)
48 def _save(self, expiration_time):
51 q = SqlQuery(self._db, 'sessions', SESSION_COLUMNS, trans=True)
52 q.delete({'id': self.id})
53 data = pickle.dumps((self._data, expiration_time), self._proto)
54 q.insert((self.id, base64.b64encode(data), expiration_time))
56 except Exception: # pylint: disable=broad-except
62 q = SqlQuery(self._db, 'sessions', SESSION_COLUMNS)
63 q.delete({'id': self.id})
65 # copy what RamSession does for now
66 def acquire_lock(self):
67 """Acquire an exclusive lock on the currently-loaded session data."""
69 self.locks.setdefault(self.id, threading.RLock()).acquire()
71 def release_lock(self):
72 """Release the lock on the currently-loaded session data."""
73 self.locks[self.id].release()