Code

svn repository setup
[roundup.git] / roundup / backends / sessions_dbm.py
index 2405a5c2d6306cdd65c035cc276e1ef5c5498a3f..1087967b6096a6d8d0e8e311238c19c378a488e0 100644 (file)
@@ -1,4 +1,4 @@
-#$Id: sessions_dbm.py,v 1.5 2004-03-31 23:08:38 richard Exp $
+#$Id: sessions_dbm.py,v 1.10 2008-08-18 05:04:01 richard Exp $
 """This module defines a very basic store that's used by the CGI interface
 to store session and one-time-key information.
 
@@ -8,6 +8,8 @@ class. It's now also used for One Time Key handling too.
 __docformat__ = 'restructuredtext'
 
 import anydbm, whichdb, os, marshal, time
+from roundup import hyperdb
+from roundup.i18n import _
 
 class BasicDatabase:
     ''' Provide a nice encapsulation of an anydbm store.
@@ -19,8 +21,7 @@ class BasicDatabase:
     def __init__(self, db):
         self.config = db.config
         self.dir = db.config.DATABASE
-        # ensure files are group readable and writable
-        os.umask(0002)
+        os.umask(db.config.UMASK)
 
     def exists(self, infoid):
         db = self.opendb('c')
@@ -45,7 +46,8 @@ class BasicDatabase:
         if os.path.exists(path):
             db_type = whichdb.whichdb(path)
             if not db_type:
-                raise hyperdb.DatabaseError, "Couldn't identify database type"
+                raise hyperdb.DatabaseError, \
+                    _("Couldn't identify database type")
         elif os.path.exists(path+'.db'):
             # if the path ends in '.db', it's a dbm database, whether
             # anydbm says it's dbhash or not!
@@ -131,17 +133,21 @@ class BasicDatabase:
         pass
 
     def updateTimestamp(self, sessid):
-        self.set(sessid, __timestamp=time.time())
-
-    def clean(self, now):
-        """Age sessions, remove when they haven't been used for a week.
-        """
+        ''' don't update every hit - once a minute should be OK '''
+        sess = self.get(sessid, '__timestamp', None)
+        now = time.time()
+        if sess is None or now > sess + 60:
+            self.set(sessid, __timestamp=now)
+
+    def clean(self):
+        ''' Remove session records that haven't been used for a week. '''
+        now = time.time()
         week = 60*60*24*7
         for sessid in self.list():
             sess = self.get(sessid, '__timestamp', None)
             if sess is None:
-                sess=time.time()
                 self.updateTimestamp(sessid)
+                continue
             interval = now - sess
             if interval > week:
                 self.destroy(sessid)
@@ -152,3 +158,4 @@ class Sessions(BasicDatabase):
 class OneTimeKeys(BasicDatabase):
     name = 'otks'
 
+# vim: set sts ts=4 sw=4 et si :