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
1 changed files with 3 additions and 2 deletions

View File

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