41 lines
1.6 KiB
Python
41 lines
1.6 KiB
Python
def pickle_cache(mode="auto"):
|
|
|
|
def pc_decorator(func):
|
|
|
|
def pc_wrapper(*args, **kwargs):
|
|
cache_fp = Path(PROXY_DIR, f"{func.__name__}.pickle")
|
|
today = dt.date.today()
|
|
if cache_fp.exists():
|
|
statinfo = cache_fp.stat()
|
|
# (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime) = statinfo
|
|
(_, _, _, _, _, _, _, _, st_mtime, _) = statinfo
|
|
mdate = dt.datetime.fromtimestamp(st_mtime).date()
|
|
else:
|
|
mdate = None
|
|
if mode == "auto":
|
|
if cache_fp.exists() and mdate == today:
|
|
action = "cached"
|
|
else:
|
|
action = "query"
|
|
else:
|
|
action = mode
|
|
if action == "cached" :
|
|
LOG.info(f"{func.__name__}: loading cached data...")
|
|
with cache_fp.open("rb") as fh:
|
|
rc = pickle.load(fh)
|
|
elif action == "query":
|
|
LOG.info(f"{func.__name__}: database access...")
|
|
rc = func(*args, **kwargs)
|
|
if cache_fp.exists() and mdate != today and Path(PROXY_DIR, "Archive").exists():
|
|
archive_fp = Path(PROXY_DIR, "Archive", f"{func.__name__}-{mdate:%Y%m%d}.pickle")
|
|
shutil.copy(cache_fp, archive_fp)
|
|
with cache_fp.open("wb") as fh:
|
|
pickle.dump(rc, fh)
|
|
else:
|
|
LOG.error(f"{func.__name__}: mode {action} unknown")
|
|
return rc
|
|
|
|
return pc_wrapper
|
|
|
|
return pc_decorator
|