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

@ -115,7 +115,7 @@ class ParseOptions:
class Cache:
""" Light, error-prone caching system. """
def __init__(self, folder, key):
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,