core (memory/cache): added some metadata:

- creation/last_access are now set
- use filename to guess MIME type when suitable
This commit is contained in:
Goffi 2020-03-01 18:31:45 +01:00
commit dfbdf8e455

View file

@ -157,7 +157,9 @@ class Cache(object):
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 @@ class Cache(object):
@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 @@ class Cache(object):
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,