Improve Cache()

Add the ability to create new Cache() objects from the current one (in the same directory). Allow the storage of int() (via str()). Don't store the key in the feed (useless, and not keeping it might make this service a little more less privacy-infrighting).
master
pictuga 2013-11-03 13:26:56 +01:00
parent 09ba8ca644
commit 5737adc253
1 changed files with 8 additions and 2 deletions

View File

@ -146,7 +146,7 @@ class Cache:
return None
def set(self, key, content):
self._cache[key] = b64encode(content or '')
self._cache[key] = b64encode(str(content) or '')
def save(self):
if len(self._cache) == 0:
@ -155,7 +155,6 @@ class Cache:
out = []
for (key, bdata) in self._cache.iteritems():
out.append(str(key) + "\t" + bdata)
out.append("_key\t" + self._key)
txt = "\n".join(out)
if not os.path.exists(self._dir):
@ -170,6 +169,13 @@ class Cache:
return time.time() - os.path.getmtime(self._file) < sec
def new(self, key):
""" Returns a Cache object in the same directory """
if key != self._key:
return Cache(self._dir, key)
else:
return self
class SimpleDownload(urllib2.HTTPCookieProcessor):
"""
Custom urllib2 handler to download a page, using etag/last-modified headers,