From 46a8728d7cd790ee358c98b6e9f1629aa9fa2a82 Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 6 Mar 2020 18:19:03 +0100 Subject: [PATCH] core (memory/cache): new removeFromCache method --- sat/memory/cache.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sat/memory/cache.py b/sat/memory/cache.py index d28fb17f..be2fc459 100644 --- a/sat/memory/cache.py +++ b/sat/memory/cache.py @@ -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