core (memory/cache): new removeFromCache method

This commit is contained in:
Goffi 2020-03-06 18:19:03 +01:00
commit 46a8728d7c

View file

@ -184,6 +184,34 @@ class Cache(object):
if metadata is not None:
return metadata["path"]
def removeFromCache(self, uid, metadata=None):
"""Remove data from cache
@param uid(unicode): unique identifier cache file
"""
cache_data = self.getMetadata(uid, update_eol=False)
if cache_data is None:
log.debug(f"cache with uid {uid!r} has already expired or been removed")
return
try:
filename = cache_data['filename']
except KeyError:
log.warning(_("missing filename for cache {uid!r}") .format(uid=uid))
else:
filepath = self.getPath(filename)
try:
filepath.unlink()
except FileNotFoundError:
log.warning(
_("missing file referenced in cache {uid!r}: {filename}")
.format(uid=uid, filename=filename)
)
cache_file = self.getPath(uid)
cache_file.unlink()
log.debug(f"cache with uid {uid!r} has been removed")
def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
"""create cache metadata and file object to use for actual data