diff src/memory/cache.py @ 2517:cd7a53c31eb6

core (memory/cache): new getMetadata method to retrieve metadata without opening the file
author Goffi <goffi@goffi.org>
date Wed, 14 Mar 2018 08:05:55 +0100
parents d485e9416493
children
line wrap: on
line diff
--- a/src/memory/cache.py	Wed Mar 14 07:57:04 2018 +0100
+++ b/src/memory/cache.py	Wed Mar 14 08:05:55 2018 +0100
@@ -59,13 +59,15 @@
             raise exceptions.DataError(u"Invalid char found")
         return os.path.join(self.cache_dir, filename)
 
-    def getFilePath(self, uid):
-        """retrieve absolute path to file
+    def getMetadata(self, uid):
+        """retrieve metadata for cached data
 
         @param uid(unicode): unique identifier of file
-        @return (unicode, None): absolute path to cached file
+        @return (dict, None): metadata with following keys:
+            see [cacheData] for data details, an additional "path" key is the full path to cached file.
             None if file is not in cache (or cache is invalid)
         """
+
         uid = uid.strip()
         if not uid:
             raise exceptions.InternalError(u"uid must not be empty")
@@ -93,7 +95,19 @@
                 time.time() - eol))
             return None
 
-        return self.getPath(cache_data['filename'])
+        cache_data['path'] = self.getPath(cache_data['filename'])
+        return cache_data
+
+    def getFilePath(self, uid):
+        """retrieve absolute path to file
+
+        @param uid(unicode): unique identifier of file
+        @return (unicode, None): absolute path to cached file
+            None if file is not in cache (or cache is invalid)
+        """
+        metadata = self.getMetadata(uid)
+        if metadata is not None:
+            return metadata['path']
 
     def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
         """create cache metadata and file object to use for actual data