-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCachedMethods.py
65 lines (50 loc) · 1.55 KB
/
CachedMethods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
""" This module keeps track of all cacheable methods (or functions) for NCATS project.
"""
__author__ = ""
__copyright__ = ""
__credits__ = []
__license__ = ""
__version__ = ""
__maintainer__ = ""
__email__ = ""
__status__ = "Prototype"
from functools import lru_cache
__all__ = ['register', 'cache_info', 'cache_clear']
# all methods (or function) decorated with `@CachedMethod.register` will be added to this list
cached_methods = []
enabled = True
lru_cache_setting = {
"maxsize": 1024,
"typed": False
}
def register(method):
"""
Put the lru_cache enabled method (or function) into `cached_methods`
:param method: the method (or function) wrapped by `@functools.lru_cache`
that you want to be managed by this module
:return:
"""
global enabled, cached_methods, lru_cache_setting
if enabled:
method = lru_cache(**lru_cache_setting)(method)
cached_methods.append(method)
return method
else:
return method
def cache_info():
"""
Call `.cache_info()` for each method in `cached_methods`; return the aggregated string, one line for one method
"""
global cached_methods
if len(cached_methods) == 0:
return None
else:
info_list = ["[{}] {}".format(method.__qualname__, method.cache_info()) for method in cached_methods]
return '\n'.join(info_list)
def cache_clear():
"""
Clear all the caches of all the methods in `cached_methods`
"""
global cached_methods
for method in cached_methods:
method.cache_clear()