11
11
12
12
from common .cache .mem_cache import MemCache
13
13
14
- lock = threading .Lock ()
14
+ _lock = threading .Lock ()
15
15
16
16
17
17
class ModelManage :
@@ -20,26 +20,27 @@ class ModelManage:
20
20
21
21
@staticmethod
22
22
def get_model (_id , get_model ):
23
- # 获取锁
24
- lock .acquire ()
25
- try :
26
- model_instance = ModelManage .cache .get (_id )
27
- if model_instance is None or not model_instance .is_cache_model ():
23
+ model_instance = ModelManage .cache .get (_id )
24
+ if model_instance is None :
25
+ with _lock :
28
26
model_instance = get_model (_id )
29
- ModelManage .cache .set (_id , model_instance , timeout = 60 * 30 )
27
+ ModelManage .cache .set (_id , model_instance , timeout = 60 * 60 * 8 )
28
+ ModelManage .clear_timeout_cache ()
29
+ return model_instance
30
+ else :
31
+ if model_instance .is_cache_model ():
32
+ ModelManage .cache .touch (_id , timeout = 60 * 60 * 8 )
33
+ return model_instance
34
+ else :
35
+ model_instance = get_model (_id )
36
+ ModelManage .cache .set (_id , model_instance , timeout = 60 * 60 * 8 )
30
37
return model_instance
31
- # 续期
32
- ModelManage .cache .touch (_id , timeout = 60 * 30 )
33
- ModelManage .clear_timeout_cache ()
34
- return model_instance
35
- finally :
36
- # 释放锁
37
- lock .release ()
38
38
39
39
@staticmethod
40
40
def clear_timeout_cache ():
41
- if time .time () - ModelManage .up_clear_time > 60 :
42
- ModelManage .cache .clear_timeout_data ()
41
+ if time .time () - ModelManage .up_clear_time > 60 * 60 :
42
+ threading .Thread (target = lambda : ModelManage .cache .clear_timeout_data ()).start ()
43
+ ModelManage .up_clear_time = time .time ()
43
44
44
45
@staticmethod
45
46
def delete_key (_id ):
0 commit comments