-
Notifications
You must be signed in to change notification settings - Fork 7
/
mwt.py
59 lines (41 loc) · 1.39 KB
/
mwt.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
# source: http://code.activestate.com/recipes/325905-memoize-decorator-with-timeout/#c1
import logging
import time
from functools import wraps
logger = logging.getLogger(__name__)
class MWT:
"""Memoize With Timeout"""
_caches = {}
_timeouts = {}
def __init__(self, timeout=2):
self.timeout = timeout
def collect(self):
"""Clear cache of results which have timed out"""
t = time.time()
for func in self._caches:
cache = {}
for key in self._caches[func]:
if (t - self._caches[func][key][1]) < self._timeouts[func]:
cache[key] = self._caches[func][key]
self._caches[func] = cache
def __call__(self, f):
cache = self._caches[f] = {}
self._timeouts[f] = self.timeout
@wraps(f)
def func(*args, **kwargs):
kw = sorted(kwargs.items())
key = (args, tuple(kw))
t = time.time()
try:
v = cache[key]
logger.debug("cache: hit")
if (t - v[1]) > self.timeout:
raise KeyError
except KeyError:
logger.debug("cache: new")
v = cache[key] = f(*args, **kwargs), t
return v[0]
def clear_cache():
self._caches[f].clear()
func.clear_cache = clear_cache
return func