changeset 3198:08151c103636

core (memory/cache): added some metadata: - creation/last_access are now set - use filename to guess MIME type when suitable
author Goffi <goffi@goffi.org>
date Sun, 01 Mar 2020 18:31:45 +0100 (2020-03-01)
parents f4a28767ec35
children 5afd7416ca2d
files sat/memory/cache.py
diffstat 1 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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,