Change Cache to use a TemporaryDirectory for its storage to

ensure and simplify thorough cleanups upon shutdown.

Thanks to Ghost for making me aware of this possibility!
This commit is contained in:
Solderpunk 2023-11-15 18:19:22 +01:00
parent 048b04bed2
commit d2fe381c3e
2 changed files with 2 additions and 7 deletions

View File

@ -1422,7 +1422,6 @@ current gemini browsing session."""
# Close TOFU DB
self.tofu_store.close()
# Clean up after ourself
self.cache.empty()
os.unlink(self.raw_file_buffer)
os.unlink(self.rendered_file_buffer)

View File

@ -16,6 +16,7 @@ class Cache:
self.cache = {}
self.cache_timestamps = {}
self.tempdir = tempfile.TemporaryDirectory()
def check(self, url):
if url not in self.cache:
@ -37,7 +38,7 @@ class Cache:
def add(self, url, mime, filename):
# Copy client's buffer file to new cache file
tmpf = tempfile.NamedTemporaryFile(delete=False)
tmpf = tempfile.NamedTemporaryFile(dir=self.tempdir.name, delete=False)
tmpf.close()
shutil.copyfile(filename, tmpf.name)
# Remember details
@ -68,11 +69,6 @@ class Cache:
def get(self, url):
return self.cache[url]
def empty(self):
for mime, filename in self.cache.values():
if os.path.exists(filename):
os.unlink(filename)
def validatecache(self):
assert self.cache.keys() == self.cache_timestamps.keys()
for _, filename in self.cache.values():