{Patch} use bsddb to store sessions

7 views
Skip to first unread message

Bibby

unread,
Oct 21, 2008, 1:19:28 AM10/21/08
to web.py
Hi, all.

Below is patch to use bsddb to store sessions.

Note: Patch author is jhuang...@gmail.com, 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:

Daniel Yang

unread,
Oct 21, 2008, 1:29:01 AM10/21/08
to we...@googlegroups.com
cool.
--
-----------------------------------------------
Yours
Faithfully

Daniel Yang

Bibby

unread,
Oct 21, 2008, 1:30:09 AM10/21/08
to web.py
Usage (like DiskStore):

session = web.session.Session(app, web.session.BDBStore('sessions'),)

Anand Chitipothu

unread,
Oct 21, 2008, 7:57:03 AM10/21/08
to we...@googlegroups.com
>
> Below is patch to use bsddb to store sessions.

module bsddb is deprecated since python 2.6.

http://docs.python.org/library/bsddb.html

Bibby

unread,
Oct 21, 2008, 8:17:32 AM10/21/08
to web.py


On Oct 21, 7:57 pm, "Anand Chitipothu" <anandol...@gmail.com> wrote:
> > Below is patch to use bsddb to store sessions.
>
> module bsddb is deprecated since python 2.6.

RHEL/CentOS 5.x ships Python-2.4.x, and it's life cycle is 7 years,
there are 5 years remain.
So why not merge this patch for these enterprise linux users?

paul jobs

unread,
Oct 21, 2008, 7:12:35 PM10/21/08
to we...@googlegroups.com
 
anand psycopg2 is not part of standard python distribution but i think it is one of the most used parts of webpy
 
bsddb becomes a 3rd party package

 
Reply all
Reply to author
Forward
0 new messages