aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/decorators.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/decorators.py')
-rw-r--r--lib/python/qmk/decorators.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/python/qmk/decorators.py b/lib/python/qmk/decorators.py
index f8f2facb1..629402b09 100644
--- a/lib/python/qmk/decorators.py
+++ b/lib/python/qmk/decorators.py
@@ -2,6 +2,7 @@
2""" 2"""
3import functools 3import functools
4from pathlib import Path 4from pathlib import Path
5from time import monotonic
5 6
6from milc import cli 7from milc import cli
7 8
@@ -84,3 +85,38 @@ def automagic_keymap(func):
84 return func(*args, **kwargs) 85 return func(*args, **kwargs)
85 86
86 return wrapper 87 return wrapper
88
89
90def lru_cache(timeout=10, maxsize=128, typed=False):
91 """Least Recently Used Cache- cache the result of a function.
92
93 Args:
94
95 timeout
96 How many seconds to cache results for.
97
98 maxsize
99 The maximum size of the cache in bytes
100
101 typed
102 When `True` argument types will be taken into consideration, for example `3` and `3.0` will be treated as different keys.
103 """
104 def wrapper_cache(func):
105 func = functools.lru_cache(maxsize=maxsize, typed=typed)(func)
106 func.expiration = monotonic() + timeout
107
108 @functools.wraps(func)
109 def wrapped_func(*args, **kwargs):
110 if monotonic() >= func.expiration:
111 func.expiration = monotonic() + timeout
112
113 func.cache_clear()
114
115 return func(*args, **kwargs)
116
117 wrapped_func.cache_info = func.cache_info
118 wrapped_func.cache_clear = func.cache_clear
119
120 return wrapped_func
121
122 return wrapper_cache