def throttled(duration): """ Syntax sugar over timecache decorator With accent on throttling calls and not actual caching of values Concurrent callers will block if function is executing, since they might depend on side effect of function call """ from easypy.caching import timecache return timecache(expiration=duration)
class UnnecessaryFunctionCall(Exception): pass @timecache(ignored_keywords='x') def test(x): nonlocal value_generated if value_generated: raise UnnecessaryFunctionCall() value_generated = True return True MultiObject(range(10)).call(lambda x: test(x=x)) @pytest.mark.parametrize('cache_decorator', [cached_property, timecache()]) def test_caching_gc_leaks(cache_decorator): """ Make sure that the cache does not prevent GC collection once the original objects die """ class Leaked(): pass class Foo: @cache_decorator def cached_method(self): return Leaked() def get(self): """Generalize property type and function type caches""" result = self.cached_method
def timecache(self): return timecache(expiration=1, get_ts_func=lambda: self.ts)