From cbdb61a918428d1a1997915b4b7ed3ffea9a862c Mon Sep 17 00:00:00 2001 From: pictuga Date: Sat, 21 Dec 2013 17:05:12 +0100 Subject: [PATCH] Allow real python dict in Cache Makes it possible to store things a nicer way --- morss.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/morss.py b/morss.py index 38e9a97..666fe0b 100644 --- a/morss.py +++ b/morss.py @@ -126,9 +126,10 @@ class ParseOptions: class Cache: """ Light, error-prone caching system. """ - def __init__(self, folder, key, persistent=False): + def __init__(self, folder, key, persistent=False, dic=False): self._key = key self._dir = folder + self._dic = dic maxsize = os.statvfs('./').f_namemax - len(self._dir) - 1 self._hash = urllib.quote_plus(self._key)[:maxsize] @@ -159,11 +160,18 @@ class Cache: self._cache[key] = self._cached[key] return self._cached[key] else: - return None + if self._dic: + self._cache[key] = {} + return self._cache[key] + else: + return None def set(self, key, content): self._cache[key] = content + __getitem__ = get + __setitem__ = set + def save(self): if len(self._cache) == 0: return @@ -182,10 +190,10 @@ class Cache: return time.time() - os.path.getmtime(self._file) < sec - def new(self, key, persistent=False): + def new(self, key, persistent=False, dic=False): """ Returns a Cache object in the same directory """ if key != self._key: - return Cache(self._dir, key, persistent) + return Cache(self._dir, key, persistent, dic) else: return self