Skip to content

Commit 873917e

Browse files
committed
perf: model manage
1 parent 6a0a57e commit 873917e

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

apps/common/config/embedding_config.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,56 @@
66
@date:2023/10/23 16:03
77
@desc:
88
"""
9+
910
import threading
1011
import time
1112

1213
from common.cache.mem_cache import MemCache
1314

14-
lock = threading.Lock()
15+
_lock = threading.Lock()
16+
locks = {}
1517

1618

1719
class ModelManage:
1820
cache = MemCache('model', {})
1921
up_clear_time = time.time()
2022

23+
@staticmethod
24+
def _get_lock(_id):
25+
lock = locks.get(_id)
26+
if lock is None:
27+
with _lock:
28+
lock = locks.get(_id)
29+
if lock is None:
30+
lock = threading.Lock()
31+
locks[_id] = lock
32+
33+
return lock
34+
2135
@staticmethod
2236
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():
37+
model_instance = ModelManage.cache.get(_id)
38+
if model_instance is None:
39+
lock = ModelManage._get_lock(_id)
40+
with lock:
41+
model_instance = ModelManage.cache.get(_id)
42+
if model_instance is None:
43+
model_instance = get_model(_id)
44+
ModelManage.cache.set(_id, model_instance, timeout=60 * 60 * 8)
45+
else:
46+
if model_instance.is_cache_model():
47+
ModelManage.cache.touch(_id, timeout=60 * 60 * 8)
48+
else:
2849
model_instance = get_model(_id)
29-
ModelManage.cache.set(_id, model_instance, timeout=60 * 30)
30-
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()
50+
ModelManage.cache.set(_id, model_instance, timeout=60 * 60 * 8)
51+
ModelManage.clear_timeout_cache()
52+
return model_instance
3853

3954
@staticmethod
4055
def clear_timeout_cache():
41-
if time.time() - ModelManage.up_clear_time > 60:
42-
ModelManage.cache.clear_timeout_data()
56+
if time.time() - ModelManage.up_clear_time > 60 * 60:
57+
threading.Thread(target=lambda: ModelManage.cache.clear_timeout_data()).start()
58+
ModelManage.up_clear_time = time.time()
4359

4460
@staticmethod
4561
def delete_key(_id):

apps/common/utils/rsa_util.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,12 @@ def generate():
4040
def get_key_pair():
4141
rsa_value = rsa_cache.get(cache_key)
4242
if rsa_value is None:
43-
lock.acquire()
44-
rsa_value = rsa_cache.get(cache_key)
45-
if rsa_value is not None:
46-
return rsa_value
47-
try:
43+
with lock:
44+
rsa_value = rsa_cache.get(cache_key)
45+
if rsa_value is not None:
46+
return rsa_value
4847
rsa_value = get_key_pair_by_sql()
4948
rsa_cache.set(cache_key, rsa_value)
50-
finally:
51-
lock.release()
5249
return rsa_value
5350

5451

0 commit comments

Comments
 (0)