From 6c72a6e5838f1ee06f57b55d332aca88d5288cb9 Mon Sep 17 00:00:00 2001 From: pictuga Date: Sat, 9 Nov 2013 18:38:02 +0100 Subject: [PATCH] Added "persistent" mode in Cache Keep not-reused vars in cache --- morss.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/morss.py b/morss.py index 3a75e38..d20f6c1 100644 --- a/morss.py +++ b/morss.py @@ -114,8 +114,8 @@ class ParseOptions: return False class Cache: - """Light, error-prone caching system.""" - def __init__(self, folder, key): + """ Light, error-prone caching system. """ + def __init__(self, folder, key, persistent=False): self._key = key self._hash = str(hash(self._key)) @@ -132,6 +132,9 @@ class Cache: key, bdata = line.split("\t", 1) self._cached[key] = b64decode(bdata) + if persistent: + self._cache = self._cached + def __del__(self): self.save() @@ -172,13 +175,16 @@ class Cache: return time.time() - os.path.getmtime(self._file) < sec - def new(self, key): + def new(self, key, persistent=False): """ Returns a Cache object in the same directory """ if key != self._key: - return Cache(self._dir, key) + return Cache(self._dir, key, persistent) else: return self + def redirect(self, key, persistent=False): + return self.__init__(self._dir, key, persistent) + class SimpleDownload(urllib2.HTTPCookieProcessor): """ Custom urllib2 handler to download a page, using etag/last-modified headers,