Added "persistent" mode in Cache

Keep not-reused vars in cache
master
pictuga 2013-11-09 18:38:02 +01:00
parent d18de37c38
commit 6c72a6e583
1 changed files with 10 additions and 4 deletions

View File

@ -114,8 +114,8 @@ class ParseOptions:
return False return False
class Cache: class Cache:
"""Light, error-prone caching system.""" """ Light, error-prone caching system. """
def __init__(self, folder, key): def __init__(self, folder, key, persistent=False):
self._key = key self._key = key
self._hash = str(hash(self._key)) self._hash = str(hash(self._key))
@ -132,6 +132,9 @@ class Cache:
key, bdata = line.split("\t", 1) key, bdata = line.split("\t", 1)
self._cached[key] = b64decode(bdata) self._cached[key] = b64decode(bdata)
if persistent:
self._cache = self._cached
def __del__(self): def __del__(self):
self.save() self.save()
@ -172,13 +175,16 @@ class Cache:
return time.time() - os.path.getmtime(self._file) < sec 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 """ """ Returns a Cache object in the same directory """
if key != self._key: if key != self._key:
return Cache(self._dir, key) return Cache(self._dir, key, persistent)
else: else:
return self return self
def redirect(self, key, persistent=False):
return self.__init__(self._dir, key, persistent)
class SimpleDownload(urllib2.HTTPCookieProcessor): class SimpleDownload(urllib2.HTTPCookieProcessor):
""" """
Custom urllib2 handler to download a page, using etag/last-modified headers, Custom urllib2 handler to download a page, using etag/last-modified headers,