-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlru_cache.py
More file actions
27 lines (20 loc) · 742 Bytes
/
Copy pathlru_cache.py
File metadata and controls
27 lines (20 loc) · 742 Bytes
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
# coding=utf-8
import functools
import time
def lru_cache(timeout: int, *lru_args, **lru_kwargs):
def wrapper_cache(func):
func = functools.lru_cache(*lru_args, **lru_kwargs)(func)
func.delta = timeout
func.expiration = time.monotonic() + func.delta
@functools.wraps(func)
def wrapped_func(*args, **kwargs):
if time.monotonic() >= func.expiration:
func.cache_clear()
func.expiration = time.monotonic() + func.delta
return func(*args, **kwargs)
wrapped_func.cache_info = func.cache_info
wrapped_func.cache_clear = func.cache_clear
return wrapped_func
return wrapper_cache
if __name__ == '__main__':
pass