# HG changeset patch # User Goffi # Date 1582750995 -3600 # Node ID a15773c6c27307ace373f3977189e97761f74174 # Parent d92a144f35895aa3669a7fe6936a70d8b1e127d2 memory(cache): extend EOL when a file metadata is retrieved diff -r d92a144f3589 -r a15773c6c273 sat/core/constants.py --- 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") diff -r d92a144f3589 -r a15773c6c273 sat/memory/cache.py --- 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)