From: khamidou Date: Tue, 9 Mar 2010 06:38:43 +0000 (+0100) Subject: fixed an embarassing assignment bug X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=e183b02c590f2ceee206d3ee25af4aa75491b562;p=cabinet.git fixed an embarassing assignment bug --- diff --git a/README b/README index a4f0c67..1237dcf 100644 --- a/README +++ b/README @@ -1,5 +1,5 @@ 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 ================ diff --git a/cabinet.py b/cabinet.py index c6aaba0..af05ca7 100644 --- a/cabinet.py +++ b/cabinet.py @@ -10,18 +10,25 @@ except ImportError: 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()