MCPcopy
hub / github.com/omkarcloud/botasaurus / wrapper_cache

Function wrapper_cache

botasaurus/decorator_helpers.py:34–65  ·  view source on GitHub ↗
(*args, **kwargs)

Source from the content-addressed store, hash-verified

32 def decorator_cache(func):
33 @wraps(func)
34 def wrapper_cache(*args, **kwargs):
35 nonlocal cache, expires_in, cache_storage
36
37 cache_enabled = kwargs.pop("cache", cache)
38 expires_in_val = kwargs.pop("expires_in", expires_in)
39 storage = kwargs.pop("cache_storage", cache_storage) or FileCacheStorage
40
41 if not cache_enabled:
42 return func(*args, **kwargs)
43
44 key_data = [args, kwargs]
45
46 if cache_enabled is True:
47 # Returns {"data": value} or None
48 cached = storage.get(func.__name__, key_data, expires_in_val)
49 if cached is not None:
50 return cached["data"] # Extract actual value
51
52 # Execute function
53 result = func(*args, **kwargs)
54
55 # Store result
56 if cache_enabled is True or cache_enabled == 'REFRESH':
57 if is_dont_cache(result):
58 storage.delete(func.__name__, key_data)
59 else:
60 storage.put(func.__name__, key_data, result)
61
62 if is_dont_cache(result):
63 result = result.data
64
65 return result
66
67 return wrapper_cache
68

Callers

nothing calls this directly

Calls 4

is_dont_cacheFunction · 0.85
getMethod · 0.45
deleteMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected