changeset 3210:fedec192a83f

core (memory/cache): new removeFromCache method
author Goffi <goffi@goffi.org>
date Fri, 06 Mar 2020 18:19:03 +0100 (2020-03-06)
parents f14eb24328d0
children 4252176ad993
files sat/memory/cache.py
diffstat 1 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/sat/memory/cache.py	Fri Mar 06 18:19:03 2020 +0100
+++ b/sat/memory/cache.py	Fri Mar 06 18:19:03 2020 +0100
@@ -184,6 +184,34 @@
         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