memory(cache): extend EOL when a file metadata is retrieved

This commit is contained in:
Goffi 2020-02-26 22:03:15 +01:00
commit 48c56ee57e
2 changed files with 24 additions and 12 deletions

View file

@ -380,13 +380,10 @@ class Const(object):
## 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")

View file

@ -111,12 +111,15 @@ class Cache(object):
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 @@ class Cache(object):
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 @@ class Cache(object):
)
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 @@ class Cache(object):
"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)