add option delete option to cache

This commit is contained in:
j 2015-12-11 19:27:54 +01:00
parent cbcef39ec0
commit d938091b26

View file

@ -217,6 +217,17 @@ class SQLiteCache(Cache):
conn.close() conn.close()
return r return r
def delete(self, url, data=None, headers=DEFAULT_HEADERS):
if data:
url_hash = hashlib.sha1((url + '?' + data).encode('utf-8')).hexdigest()
else:
url_hash = hashlib.sha1(url.encode('utf-8')).hexdigest()
conn = self.connect()
c = conn.cursor()
sql = 'DELETE FROM cache WHERE url_hash=?'
t = (url_hash, )
c.execute(sql, t)
def set(self, url, post_data, data, headers): def set(self, url, post_data, data, headers):
if post_data: if post_data:
url_hash = hashlib.sha1((url + '?' + post_data).encode('utf-8')).hexdigest() url_hash = hashlib.sha1((url + '?' + post_data).encode('utf-8')).hexdigest()
@ -302,6 +313,16 @@ class FileCache(Cache):
r = zlib.decompress(r) r = zlib.decompress(r)
return r return r
def delete(self, url, data=None, headers=DEFAULT_HEADERS):
if data:
url_hash = hashlib.sha1((url + '?' + data).encode('utf-8')).hexdigest()
else:
url_hash = hashlib.sha1(url.encode('utf-8')).hexdigest()
domain = ".".join(urllib.parse.urlparse(url)[1].split('.')[-2:])
prefix, i, f = self.files(domain, url_hash)
if os.path.exists(i):
os.unlink(i)
def set(self, url, post_data, data, headers): def set(self, url, post_data, data, headers):
if post_data: if post_data:
url_hash = hashlib.sha1((url + '?' + post_data).encode('utf-8')).hexdigest() url_hash = hashlib.sha1((url + '?' + post_data).encode('utf-8')).hexdigest()