Cache accepts arbitrary (lambda) condition for refreshing

This commit is contained in:
southerntofu 2021-07-29 11:06:27 -04:00 committed by metalune
parent 2eabe5c1eb
commit 2b837c887d

View file

@ -67,15 +67,16 @@ class VideoWrapper:
# Helper Class for using caches
class Cache:
def __init__(self):
def __init__(self, criteria = lambda diff: diff.days > 0):
self.dict = {}
self.criteria = criteria
def get(self, arg, func):
if arg in self.dict:
last_time_updated = (self.dict[arg])[1]
time_diff = datetime.now() - last_time_updated
if time_diff.days > 0:
if self.criteria(time_diff):
self.dict[arg] = [
func(arg),
datetime.now()