import time import functools import threading def ttl_cache(func): cache = {} lock = threading.Lock() @functools.wraps(func) def wrapper(*args, **kwargs): key = args for item in kwargs.items(): key += item key = hash(key) with lock: if key in cache: value, timestamp = cache[key] if time.time() - timestamp < 600: return value result = func(*args, **kwargs) cache[key] = (result, time.time()) return result return wrapper
标签:缓存,func,ttl,python,cache,result,key,time From: https://www.cnblogs.com/viete/p/17548269.html