# HG changeset patch # User Goffi # Date 1583083905 -3600 # Node ID 08151c103636c131491f30a95b6b1681baf9fe4b # Parent f4a28767ec3517b7c11a1e39a3ee1c8f6a96ad1e core (memory/cache): added some metadata: - creation/last_access are now set - use filename to guess MIME type when suitable diff -r f4a28767ec35 -r 08151c103636 sat/memory/cache.py --- a/sat/memory/cache.py Sun Mar 01 18:29:46 2020 +0100 +++ b/sat/memory/cache.py Sun Mar 01 18:31:45 2020 +0100 @@ -157,7 +157,9 @@ 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 + now = int(time.time()) + cache_data["last_access"] = now + cache_data["eol"] = now + max_age with cache_url.open("wb") as f: pickle.dump(cache_data, f, protocol=2) @@ -182,6 +184,7 @@ @param uid(unicode): an identifier of the file which must be unique @param mime_type(unicode): MIME type of the file to cache it will be used notably to guess file extension + It may be autogenerated if filename is specified @param max_age(int, None): maximum age in seconds the cache metadata will have an "eol" (end of life) None to use default value @@ -206,12 +209,17 @@ ext = DEFAULT_EXT mime_type = None filename = uid + ext + elif mime_type is None: + # we have filename but not MIME type, we try to guess the later + mime_type = mimetypes.guess_type(filename, strict=False)[0] if max_age is None: max_age = C.DEFAULT_MAX_AGE + now = int(time.time()) cache_data = { "source": source, "filename": filename, - "eol": int(time.time()) + max_age, + "creation": now, + "eol": now + max_age, # we also store max_age for updating eol "max_age": max_age, "mime_type": mime_type,