-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.py
84 lines (74 loc) · 2.75 KB
/
cache.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import string
from functools import wraps, partial
from collections import deque
import time
import logging
logger = logging.getLogger("gs-scroller.cache")
class TemporaryCache(dict):
def __init__(self, timeout, *, max_len=None):
self.timeout = timeout
self.cached_times = dict()
self.time_queue = deque()
self.max_len = max_len
def get_cached_value(self, key, generator):
current_time = time.time()
old_limit_time = current_time - self.timeout
cached_time = self.cached_times.get(key)
if cached_time is not None:
if cached_time >= old_limit_time:
cached_result = self.get((key, cached_time), None)
if cached_result is not None:
logger.debug("using cached value")
return cached_result[0]
try:
del self.cached_times[key]
except IndexError:
pass
self.cleanup_old_cache(old_limit_time)
return self.set_cached_value(key, current_time, generator())
def cleanup_old_cache(self, old_limit_time):
while True:
if not self.evict_oldest_cache(old_limit_time):
break
def evict_oldest_cache(self, old_limit_time=None):
try:
(old_key, old_cached_time) = self.time_queue.pop()
except IndexError:
return False
if old_limit_time is not None and old_cached_time > old_limit_time:
self.time_queue.append((old_key, old_cached_time))
return False
try:
del self[old_key, old_cached_time]
except IndexError:
pass
cached_time = self.cached_times.get(old_key)
if cached_time is old_cached_time:
try:
del self.cached_times[old_key]
except IndexError:
pass
return True
def set_cached_value(self, key, current_time, value):
while len(self.time_queue) >= self.max_len:
if not self.evict_oldest_cache():
break
logger.debug("cacheing value")
self[key, current_time] = (value,)
self.cached_times[key] = current_time
self.time_queue.appendleft((key, current_time))
return value
def temporary_cache(timeout):
"""
Decorator. Implement cacheing of function results.
Function arguments must always be strings, and never contain slash.
"""
cache = TemporaryCache(timeout, max_len=100)
def wrapper(function, timeout=timeout, cache=cache):
@wraps(function)
def wrapped(*args):
generator = partial(function, *args)
key = '/'.join(args)
return cache.get_cached_value(key, generator)
return wrapped
return wrapper