Hi, all.

Below is patch to use bsddb to store sessions.

Note: Patch author is [EMAIL PROTECTED], not me. But he agreed to
public this patch under the license which webpy used.

Refer google group 'python-cn' (thread is in Chinese):
http://groups.google.com/group/python-cn/browse_thread/thread/6b995c68193223b6/c21a04077e9db7be#c21a04077e9db7be

Download link:
http://python-cn.googlegroups.com/attach/2044891ef481e378/webpy-session-BDBStore.patch

diff --git a/web/session.py b/web/session.py
index 996b58f..e5b6956 100644
--- a/web/session.py
+++ b/web/session.py
@@ -3,7 +3,7 @@ Session Management
 (from web.py)
 """

-import os, time, datetime, random, base64
+import os, time, datetime, random, base64, bsddb
 try:
     import cPickle as pickle
 except ImportError:
@@ -221,6 +221,60 @@ class DiskStore(Store):
             if now - atime > timeout :
                 os.remove(path)

+class BDBStore(Store):
+    """Store for saving a session in bsddb
+
+        >>> import tempfile
+        >>> root = tempfile.mkdtemp()
+        >>> s = BDBStore(root)
+        >>> s['a'] = 'foo'
+        >>> s['a']
+        'foo'
+        >>> time.sleep(0.01)
+        >>> s.cleanup(0.01)
+        >>> s['a']
+        Traceback (most recent call last):
+            ...
+        KeyError: 'a'
+    """
+    def __init__(self, root):
+        if hasattr(root, '__getitem__') and hasattr(root,
'__setitem__'):
+            self.db = root
+            return
+        if not os.path.isdir(root):
+            os.makedirs(root)
+        dbenv = bsddb.db.DBEnv()
+        #dbenv.set_shm_key(23)
+        dbenv.open(root, bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL
| bsddb.db.DB_THREAD)
+        d = bsddb.db.DB(dbenv)
+        d.open('sessions.db', bsddb.db.DB_BTREE, bsddb.db.DB_CREATE,
0666)
+        self.db = bsddb._DBWithCursor(d)
+
+    def __contains__(self, key):
+        return key in self.db
+
+    def __getitem__(self, key):
+        if key in self.db:
+            return pickle.loads(self.db[key])[1]
+        else:
+            raise KeyError, key
+
+    def __setitem__(self, key, value):
+        self.db[key] = pickle.dumps((time.time(), value))
+
+    def __delitem__(self, key):
+        if key in self.db:
+            del self.db[key]
+
+    def cleanup(self, timeout):
+        if not hasattr(self.db, 'iteritems'):
+            return
+        now = time.time()
+        db = self.db
+        for key, value in db.iteritems():
+            if now - pickle.loads(value[0]):
+                del db[key]
+
 class DBStore(Store):
     """Store for saving a session in database
     Needs a table with the following columns:
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to