|
1 | 1 | # coding=utf-8 |
2 | 2 | """ |
3 | | - @project: maxkb |
4 | | - @Author:虎 |
5 | | - @file: embedding_config.py |
6 | | - @date:2023/10/23 16:03 |
7 | | - @desc: |
| 3 | +@project: maxkb |
| 4 | +@Author:虎 |
| 5 | +@file: embedding_config.py |
| 6 | +@date:2023/10/23 16:03 |
| 7 | +@desc: |
8 | 8 | """ |
9 | 9 |
|
10 | 10 | import threading |
11 | 11 | import time |
| 12 | +import json |
12 | 13 |
|
13 | 14 | from common.cache.mem_cache import MemCache |
| 15 | +from common.utils.rsa_util import rsa_long_decrypt |
14 | 16 |
|
15 | 17 | _lock = threading.Lock() |
16 | 18 | locks = {} |
17 | 19 |
|
18 | 20 |
|
19 | 21 | class ModelManage: |
20 | | - cache = MemCache('model', {}) |
| 22 | + cache = MemCache("model", {}) |
| 23 | + # 按 model_id 缓存的解密凭据与模型行,避免每次调用重复 RSA 解密 / 重复查库。 |
| 24 | + # 二者在模型更新/删除时通过 delete_key(_id) 一并失效,保证改 key 立即生效。 |
| 25 | + credential_cache = MemCache("model_credential", {}) |
| 26 | + model_cache = MemCache("model_row", {}) |
21 | 27 | up_clear_time = time.time() |
22 | 28 |
|
| 29 | + @staticmethod |
| 30 | + def get_decrypted_credential(model_id, credential): |
| 31 | + """返回解密后的凭据 dict;结果按 model_id 缓存,改 key 时由 delete_key 清除。""" |
| 32 | + cached = ModelManage.credential_cache.get(model_id) |
| 33 | + if cached is not None: |
| 34 | + return cached |
| 35 | + decrypted = json.loads(rsa_long_decrypt(credential)) |
| 36 | + ModelManage.credential_cache.set(model_id, decrypted, timeout=60 * 60 * 8) |
| 37 | + return decrypted |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def get_model_row(_id): |
| 41 | + return ModelManage.model_cache.get(_id) |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def set_model_row(_id, model): |
| 45 | + ModelManage.model_cache.set(_id, model, timeout=60 * 60 * 8) |
| 46 | + |
23 | 47 | @staticmethod |
24 | 48 | def _get_lock(_id): |
25 | 49 | lock = locks.get(_id) |
@@ -59,24 +83,27 @@ def clear_timeout_cache(): |
59 | 83 |
|
60 | 84 | @staticmethod |
61 | 85 | def delete_key(_id): |
62 | | - if ModelManage.cache.has_key(_id): |
63 | | - ModelManage.cache.delete(_id) |
| 86 | + for cache in (ModelManage.cache, ModelManage.credential_cache, ModelManage.model_cache): |
| 87 | + if cache.has_key(_id): |
| 88 | + cache.delete(_id) |
64 | 89 |
|
65 | 90 |
|
66 | 91 | class VectorStore: |
67 | 92 | from knowledge.vector.pg_vector import PGVector |
68 | 93 | from knowledge.vector.base_vector import BaseVectorStore |
| 94 | + |
69 | 95 | instance_map = { |
70 | | - 'pg_vector': PGVector, |
| 96 | + "pg_vector": PGVector, |
71 | 97 | } |
72 | 98 | instance = None |
73 | 99 |
|
74 | 100 | @staticmethod |
75 | 101 | def get_embedding_vector() -> BaseVectorStore: |
76 | 102 | from knowledge.vector.pg_vector import PGVector |
| 103 | + |
77 | 104 | if VectorStore.instance is None: |
78 | 105 | from maxkb.const import CONFIG |
79 | | - vector_store_class = VectorStore.instance_map.get(CONFIG.get("VECTOR_STORE_NAME"), |
80 | | - PGVector) |
| 106 | + |
| 107 | + vector_store_class = VectorStore.instance_map.get(CONFIG.get("VECTOR_STORE_NAME"), PGVector) |
81 | 108 | VectorStore.instance = vector_store_class() |
82 | 109 | return VectorStore.instance |
0 commit comments