X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fsession.py;h=24d89d7e6c3a92aa33ade7a8cdde2f43b12ab35a;hb=c7cc3ff342419755855aa93f165920d4f0107aac;hp=3b2b2b29cc98dfbf435544ffb842e3af9b05d32c;hpb=b9e22c33901b627684450411477f6c459fc302da;p=wrw.git diff --git a/wrw/session.py b/wrw/session.py index 3b2b2b2..24d89d7 100644 --- a/wrw/session.py +++ b/wrw/session.py @@ -1,3 +1,4 @@ +from __future__ import with_statement import threading, time, pickle, random, os import cookie, env @@ -157,33 +158,44 @@ class db(object): 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