def wrapper(wrappingobj: CacheableObject, *args: Any, **kwargs: Any) -> Any: function_sig = f.__name__ for arg in args: function_sig += str(arg) for _, value in kwargs.items(): function_sig += str(value) # TODO: Do I really need hash() here? cache_key = hash(function_sig) with wrappingobj.lock: now = ts_now() if cache_key in wrappingobj.results_cache: cache_life_secs = now - wrappingobj.results_cache[ cache_key].timestamp cache_miss = (cache_key not in wrappingobj.results_cache or cache_life_secs >= wrappingobj.cache_ttl_secs) if cache_miss: result = f(wrappingobj, *args, **kwargs) with wrappingobj.lock: wrappingobj.results_cache[cache_key] = ResultCache( result, now) return result # else hit the cache with wrappingobj.lock: return wrappingobj.results_cache[cache_key].result
def wrapper(wrappingobj, *args): with wrappingobj.lock: now = ts_now() cache_miss = ( f.__name__ not in wrappingobj.results_cache or now - wrappingobj.results_cache[f.__name__].timestamp > seconds ) if cache_miss: result = f(wrappingobj, *args) with wrappingobj.lock: wrappingobj.results_cache[f.__name__] = ResultCache(result, now) return result # else hit the cache with wrappingobj.lock: return wrappingobj.results_cache[f.__name__].result
def wrapper(wrappingobj, *args): with wrappingobj.lock: now = ts_now() if f.__name__ in wrappingobj.results_cache: cache_life_secs = now - wrappingobj.results_cache[f.__name__].timestamp cache_miss = ( f.__name__ not in wrappingobj.results_cache or cache_life_secs > wrappingobj.cache_ttl_secs ) if cache_miss: result = f(wrappingobj, *args) with wrappingobj.lock: wrappingobj.results_cache[f.__name__] = ResultCache(result, now) return result # else hit the cache with wrappingobj.lock: return wrappingobj.results_cache[f.__name__].result
def wrapper(wrappingobj: CacheableObject, *args: Any, **kwargs: Any) -> Any: ignore_cache = kwargs.pop('ignore_cache', False) cache_key = _function_sig_key(f.__name__, False, *args, **kwargs) now = ts_now() if ignore_cache is False: # Check the cache if cache_key in wrappingobj.results_cache: cache_life_secs = now - wrappingobj.results_cache[ cache_key].timestamp cache_miss = (ignore_cache is True or cache_key not in wrappingobj.results_cache or cache_life_secs >= wrappingobj.cache_ttl_secs) if cache_miss: # Call the function, write the result in cache and return it result = f(wrappingobj, *args, **kwargs) wrappingobj.results_cache[cache_key] = ResultCache(result, now) return result # else hit the cache and return it return wrappingobj.results_cache[cache_key].result