Hi, all.
Here is a patch to use bsddb to store sessions for webpy-0.3.
Former thread:
http://groups.google.com/group/webpy/browse_thread/thread/4b1311e712a05076/f75eb816e878082f
Note: Patch author is [EMAIL PROTECTED], not me. But he agreed to
public this patch under the license which webpy used.
Note: Do not mix the directories store DiskStore session and BDBStore.
Refer google group 'python-cn' (thread is in Chinese):
http://groups.google.com/group/python-cn/browse_thread/thread/6b995c68193223b6/c21a04077e9db7be#c21a04077e9db7be
Thanks all webpy developers and contributers. :)
--
Best Regards.
Zhang Huangbin
- iRedMail: Mail Server Solution for Red Hat(R) Enterprise Linux & CentOS 5.x:
http://code.google.com/p/iredmail/
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
--- web/session.py 2008-12-06 18:04:13.000000000 +0800
+++ /home/michaelz/session.py 2008-12-06 21:37:47.000000000 +0800
@@ -3,7 +3,7 @@
(from web.py)
"""
-import os, time, datetime, random, base64
+import os, time, datetime, random, base64, bsddb
try:
import cPickle as pickle
except ImportError:
@@ -235,6 +235,60 @@
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), 1)
+
+ 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: