From 0c42f91f4ccf98a37f055afb777ed491da56335e Mon Sep 17 00:00:00 2001 From: Zach White Date: Sun, 25 Oct 2020 14:48:44 -0700 Subject: Generate api data on each push (#10609) * add new qmk generate-api command, to generate a complete set of API data. * Generate api data and push it to the keyboard repo * fix typo * Apply suggestions from code review Co-authored-by: Joel Challis * fixup api workflow * remove file-changes-action * use a more mainstream github action * fix yaml error * Apply suggestions from code review Co-authored-by: Erovia * more uniform date handling * make flake8 happy * Update lib/python/qmk/decorators.py Co-authored-by: Erovia Co-authored-by: Joel Challis Co-authored-by: Erovia --- lib/python/qmk/decorators.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/python/qmk/decorators.py') diff --git a/lib/python/qmk/decorators.py b/lib/python/qmk/decorators.py index f8f2facb1c..629402b095 100644 --- a/lib/python/qmk/decorators.py +++ b/lib/python/qmk/decorators.py @@ -2,6 +2,7 @@ """ import functools from pathlib import Path +from time import monotonic from milc import cli @@ -84,3 +85,38 @@ def automagic_keymap(func): return func(*args, **kwargs) return wrapper + + +def lru_cache(timeout=10, maxsize=128, typed=False): + """Least Recently Used Cache- cache the result of a function. + + Args: + + timeout + How many seconds to cache results for. + + maxsize + The maximum size of the cache in bytes + + typed + When `True` argument types will be taken into consideration, for example `3` and `3.0` will be treated as different keys. + """ + def wrapper_cache(func): + func = functools.lru_cache(maxsize=maxsize, typed=typed)(func) + func.expiration = monotonic() + timeout + + @functools.wraps(func) + def wrapped_func(*args, **kwargs): + if monotonic() >= func.expiration: + func.expiration = monotonic() + timeout + + func.cache_clear() + + return func(*args, **kwargs) + + wrapped_func.cache_info = func.cache_info + wrapped_func.cache_clear = func.cache_clear + + return wrapped_func + + return wrapper_cache -- cgit v1.2.3