import threading, time, pickle, random, os
-import cookie, env
+from . import cookie, env
__all__ = ["db", "get"]
def gennonce(length):
nonce = ""
- for i in xrange(length):
+ for i in range(length):
nonce += chr(random.randint(0, 255))
return nonce
def clean(self):
now = int(time.time())
with self.lock:
- clist = self.live.keys()
+ clist = list(self.live.keys())
for sessid in clist:
with self.lock:
try:
else:
raise Exception("Illegal session entry: " + repr(entry[1]))
- def fetch(self, req):
- now = int(time.time())
- sessid = cookie.get(req, self.cookiename)
- new = False
+ def checkclean(self):
with self.lock:
if self.cthread is None:
self.cthread = threading.Thread(target = self.cleanloop)
self.cthread.setDaemon(True)
self.cthread.start()
+
+ def mksession(self, req):
+ return session(threading.RLock())
+
+ def mkcookie(self, req, sess):
+ cookie.add(req, self.cookiename, sess.id,
+ path=self.path,
+ expires=cookie.cdate(time.time() + sess.expire))
+
+ def fetch(self, req):
+ now = int(time.time())
+ sessid = cookie.get(req, self.cookiename)
+ new = False
try:
if sessid is None:
raise KeyError()
sess = self._fetch(sessid)
except KeyError:
- sess = session(threading.RLock())
+ sess = self.mksession(req)
new = True
def ckfreeze(req):
if sess.dirty():
if new:
- cookie.add(req, self.cookiename, sess.id, self.path)
+ self.mkcookie(req, sess)
with self.lock:
self.live[sess.id] = [sess.lock, sess]
try:
self.freeze(sess)
except:
pass
+ self.checkclean()
req.oncommit(ckfreeze)
return sess
data = self.backdb[sessid]
try:
return pickle.loads(data)
- except Exception, e:
+ except:
raise KeyError()
def freeze(self, sess):