perf: model manage (#3226)
This commit is contained in:
parent
6a0a57e785
commit
5f19924f9f
@ -6,40 +6,56 @@
|
|||||||
@date:2023/10/23 16:03
|
@date:2023/10/23 16:03
|
||||||
@desc:
|
@desc:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from common.cache.mem_cache import MemCache
|
from common.cache.mem_cache import MemCache
|
||||||
|
|
||||||
lock = threading.Lock()
|
_lock = threading.Lock()
|
||||||
|
locks = {}
|
||||||
|
|
||||||
|
|
||||||
class ModelManage:
|
class ModelManage:
|
||||||
cache = MemCache('model', {})
|
cache = MemCache('model', {})
|
||||||
up_clear_time = time.time()
|
up_clear_time = time.time()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_lock(_id):
|
||||||
|
lock = locks.get(_id)
|
||||||
|
if lock is None:
|
||||||
|
with _lock:
|
||||||
|
lock = locks.get(_id)
|
||||||
|
if lock is None:
|
||||||
|
lock = threading.Lock()
|
||||||
|
locks[_id] = lock
|
||||||
|
|
||||||
|
return lock
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_model(_id, get_model):
|
def get_model(_id, get_model):
|
||||||
# 获取锁
|
model_instance = ModelManage.cache.get(_id)
|
||||||
lock.acquire()
|
if model_instance is None:
|
||||||
try:
|
lock = ModelManage._get_lock(_id)
|
||||||
model_instance = ModelManage.cache.get(_id)
|
with lock:
|
||||||
if model_instance is None or not model_instance.is_cache_model():
|
model_instance = ModelManage.cache.get(_id)
|
||||||
|
if model_instance is None:
|
||||||
|
model_instance = get_model(_id)
|
||||||
|
ModelManage.cache.set(_id, model_instance, timeout=60 * 60 * 8)
|
||||||
|
else:
|
||||||
|
if model_instance.is_cache_model():
|
||||||
|
ModelManage.cache.touch(_id, timeout=60 * 60 * 8)
|
||||||
|
else:
|
||||||
model_instance = get_model(_id)
|
model_instance = get_model(_id)
|
||||||
ModelManage.cache.set(_id, model_instance, timeout=60 * 30)
|
ModelManage.cache.set(_id, model_instance, timeout=60 * 60 * 8)
|
||||||
return model_instance
|
ModelManage.clear_timeout_cache()
|
||||||
# 续期
|
return model_instance
|
||||||
ModelManage.cache.touch(_id, timeout=60 * 30)
|
|
||||||
ModelManage.clear_timeout_cache()
|
|
||||||
return model_instance
|
|
||||||
finally:
|
|
||||||
# 释放锁
|
|
||||||
lock.release()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clear_timeout_cache():
|
def clear_timeout_cache():
|
||||||
if time.time() - ModelManage.up_clear_time > 60:
|
if time.time() - ModelManage.up_clear_time > 60 * 60:
|
||||||
ModelManage.cache.clear_timeout_data()
|
threading.Thread(target=lambda: ModelManage.cache.clear_timeout_data()).start()
|
||||||
|
ModelManage.up_clear_time = time.time()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_key(_id):
|
def delete_key(_id):
|
||||||
|
|||||||
@ -40,15 +40,12 @@ def generate():
|
|||||||
def get_key_pair():
|
def get_key_pair():
|
||||||
rsa_value = rsa_cache.get(cache_key)
|
rsa_value = rsa_cache.get(cache_key)
|
||||||
if rsa_value is None:
|
if rsa_value is None:
|
||||||
lock.acquire()
|
with lock:
|
||||||
rsa_value = rsa_cache.get(cache_key)
|
rsa_value = rsa_cache.get(cache_key)
|
||||||
if rsa_value is not None:
|
if rsa_value is not None:
|
||||||
return rsa_value
|
return rsa_value
|
||||||
try:
|
|
||||||
rsa_value = get_key_pair_by_sql()
|
rsa_value = get_key_pair_by_sql()
|
||||||
rsa_cache.set(cache_key, rsa_value)
|
rsa_cache.set(cache_key, rsa_value)
|
||||||
finally:
|
|
||||||
lock.release()
|
|
||||||
return rsa_value
|
return rsa_value
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user