Skip to content

Commit 949e4de

Browse files
authored
perf: Refine the Model Manager code (#3089)
1 parent c12988b commit 949e4de

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

apps/common/config/embedding_config.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from common.cache.mem_cache import MemCache
1313

14-
lock = threading.Lock()
14+
_lock = threading.Lock()
1515

1616

1717
class ModelManage:
@@ -20,26 +20,27 @@ class ModelManage:
2020

2121
@staticmethod
2222
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:
2826
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)
3037
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()
3838

3939
@staticmethod
4040
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()
4344

4445
@staticmethod
4546
def delete_key(_id):

0 commit comments

Comments
 (0)