changeset 3188:a15773c6c273

memory(cache): extend EOL when a file metadata is retrieved
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 22:03:15 +0100
parents d92a144f3589
children 142ecb7f6338
files sat/core/constants.py sat/memory/cache.py
diffstat 2 files changed, 24 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/sat/core/constants.py	Wed Feb 26 22:03:11 2020 +0100
+++ b/sat/core/constants.py	Wed Feb 26 22:03:15 2020 +0100
@@ -380,13 +380,10 @@
     ## Misc ##
     SAVEFILE_DATABASE = APP_NAME_FILE + ".db"
     IQ_SET = '/iq[@type="set"]'
-    # Prefix used for environment variables
-    ENV_PREFIX = "SAT_"
+    ENV_PREFIX = "SAT_"  # Prefix used for environment variables
     IGNORE = "ignore"
-    # used in bridge when a integer value is expected
-    NO_LIMIT = -1
-    # default max age of cached files, in seconds
-    DEFAULT_MAX_AGE = 3600 * 24 * 14
+    NO_LIMIT = -1  # used in bridge when a integer value is expected
+    DEFAULT_MAX_AGE = 1209600  # default max age of cached files, in seconds
     HASH_SHA1_EMPTY = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
     STANZA_NAMES = ("iq", "message", "presence")
 
--- a/sat/memory/cache.py	Wed Feb 26 22:03:11 2020 +0100
+++ b/sat/memory/cache.py	Wed Feb 26 22:03:15 2020 +0100
@@ -111,12 +111,15 @@
             raise exceptions.DataError("Invalid char found")
         return self.cache_dir / filename
 
-    def getMetadata(self, uid):
+    def getMetadata(self, uid, update_eol=True):
         """Retrieve metadata for cached data
 
         @param uid(unicode): unique identifier of file
+        @param update_eol(bool): True if eol must extended
+            if True, max_age will be added to eol (only if it is not already expired)
         @return (dict, None): metadata with following keys:
-            see [cacheData] for data details, an additional "path" key is the full path to cached file.
+            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)
         """
 
@@ -124,17 +127,17 @@
         if not uid:
             raise exceptions.InternalError("uid must not be empty")
         cache_url = self.getPath(uid)
-        if not cache_url.exists:
+        if not cache_url.exists():
             return None
 
         try:
             with cache_url.open("rb") as f:
                 cache_data = pickle.load(f)
-        except IOError:
-            log.warning("can't read cache at {}".format(cache_url))
+        except IOError as e:
+            log.warning(f"can't read cache at {cache_url}: {e}")
             return None
         except pickle.UnpicklingError:
-            log.warning("invalid cache found at {}".format(cache_url))
+            log.warning(f"invalid cache found at {cache_url}")
             return None
 
         try:
@@ -148,6 +151,16 @@
             )
             return None
 
+        if update_eol:
+            try:
+                max_age = cache_data["max_age"]
+            except KeyError:
+                log.warning(f"no max_age found for cache at {cache_url}, using default")
+                max_age = cache_data["max_age"] = C.DEFAULT_MAX_AGE
+            cache_data["eol"] = int(time.time()) + max_age
+            with cache_url.open("wb") as f:
+                pickle.dump(cache_data, f, protocol=2)
+
         cache_data["path"] = self.getPath(cache_data["filename"])
         return cache_data
 
@@ -199,6 +212,8 @@
             "source": source,
             "filename": filename,
             "eol": int(time.time()) + max_age,
+            # we also store max_age for updating eol
+            "max_age": max_age,
             "mime_type": mime_type,
         }
         file_path = self.getPath(filename)