-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_meta.py
52 lines (43 loc) · 1.54 KB
/
cache_meta.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
import time
from multiprocessing import Process, Manager
from config import cache_conf
class CacheMeta(type):
cache_tout = cache_conf['cache_lifetime']
func_names = ['get_data']
cache = Manager().list()
cache_cleaner = None
def __new__(mcs, name, bases, dct):
if CacheMeta.cache_cleaner is None:
CacheMeta.cache_cleaner = Process(
target=CacheMeta.cleaner,
args=(CacheMeta.cache, CacheMeta.cache_tout)
)
CacheMeta.cache_cleaner.start()
dct['cache'] = CacheMeta.cache
for f_name in CacheMeta.func_names:
if f_name in dct:
dct[f_name] = CacheMeta.cache_func(dct[f_name])
return super(CacheMeta, mcs).__new__(mcs, name, bases, dct)
@staticmethod
def cache_func(func):
def cached(self, *args, **kwargs):
if self.cache[:]:
return self.cache[:]
else:
result = func(self, *args, **kwargs)
self.cache.extend(result)
return result
return cached
@staticmethod
def cleaner(cache, t):
while True:
try:
for i in xrange(t):
if not hasattr(cache, 'pop'):
raise AttributeError
time.sleep(1)
print('[{0}] Clearing cache'.format(time.asctime()))
while cache[:]:
cache.pop()
except (AttributeError, IOError, KeyboardInterrupt):
break