From d2b74819b4cf1610e4938841f73676590f70b416 Mon Sep 17 00:00:00 2001 From: pictuga Date: Fri, 19 Apr 2013 11:40:35 +0200 Subject: [PATCH] Improved caching. No longer writes everytime a value is added, since it could cause some issues if two instances of the script were run at the same time. Now it only writes when the Cache object is no longer in use (ie. garbage colllected). --- morss.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/morss.py b/morss.py index eaab0b4..9897663 100644 --- a/morss.py +++ b/morss.py @@ -70,20 +70,22 @@ class Cache: log(str(hash(self._key))) + def __del__(self): + self.save() + def get(self, key): if key in self._cached: return b64decode(self._cached[key]) else: return None - def save(self, key, content): - # Maybe, appending to file when adding new elements could be - # a good idea, but that'd require to check a couple of things, - # like whether it has aleardy been over-written (ie. whether - # it no longer contains self._cached) - + def set(self, key, content): self._cache[key] = b64encode(content) + if not os.path.exists(self._file): + self.save() + + def save(self): txt = "" for (key, bdata) in self._cache.iteritems(): txt += "\n" + str(key) + "\t" + bdata