Mercurial > libervia-backend
diff sat/memory/cache.py @ 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 | 554b3b632378 |
children | 08151c103636 |
line wrap: on
line diff
--- 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)