Cabinet is a very-thin layer other berkeley db. It serializes python objects to json using simplejson and then saves them to a bsddb backend.
-The library is really simple (at the moment it's less than 30 loc!) so don't hesitate to modify it and send me back patches!
+The library is really simple (at the moment it's less than 100 loc!) so don't hesitate to modify it and send me back patches!
Example session
================
class Cabinet(object):
def __init__(self, filename, *args):
self.db = bsddb.hashopen(filename)
-
+ self.cache = {}
+
def __getitem__(self, key):
- if self.db.has_key(key):
- return json.loads(self.db[key])
+ if self.cache.has_key(key):
+ return self.cache[key]
+ elif self.db.has_key(key):
+ self.cache[key] = json.loads(self.db[key])
+ return self.cache[key]
else:
raise KeyError
def __setitem__(self, key, item):
- self.db[key] = json.dumps(item)
+ self.cache[key] = item
def __delitem__(self, key):
del self.db[key]
def sync(self):
+ for key in self.cache:
+ self.db[key] = json.dumps(self.cache[key])
+
self.db.sync()