Przeglądaj źródła

优化缓存和回滚

SGTY 2 dni temu
rodzic
commit
9379ee4021

+ 1 - 1
agent/cdss/libs/cdss_helper.py

@@ -240,7 +240,7 @@ class CDSSHelper(GraphHelper):
         return True
 
     propService = KGPropService(next(get_db()))
-    cache = TTLCache(maxsize=100000, ttl=60*60*24*30)
+    cache = TTLCache(maxsize=1000, ttl=60*60*24*30)
     def cdss_travel(self, input: CDSSInput, start_nodes: List, max_hops=3):
         """
         基于输入的症状节点,在知识图谱中进行遍历,查找相关疾病、科室、检查和药品

+ 1 - 1
agent/cdss/libs/cdss_helper2.py

@@ -252,7 +252,7 @@ class CDSSHelper(GraphHelper):
         return True
 
     propService = KGPropService(next(get_db()))
-    cache = TTLCache(maxsize=100000, ttl=60*60*24*30)
+    cache = TTLCache(maxsize=1000, ttl=60*60*24*30)
     def cdss_travel(self, input: CDSSInput, start_nodes: List, max_hops=3):
         """
         基于输入的症状节点,在知识图谱中进行遍历,查找相关疾病、科室、检查和药品

+ 1 - 1
agent/cdss/libs/cdss_helper3.py

@@ -242,7 +242,7 @@ class CDSSHelper(GraphHelper):
         return True
 
     propService = KGPropService(next(get_db()))
-    cache = TTLCache(maxsize=100000, ttl=60*60*24*30)
+    cache = TTLCache(maxsize=1000, ttl=60*60*24*30)
     def cdss_travel(self, input: CDSSInput, start_nodes: List, max_hops=3):
         """
         基于输入的症状节点,在知识图谱中进行遍历,查找相关疾病、科室、检查和药品

+ 1 - 1
service/cdss_service.py

@@ -17,7 +17,7 @@ from cachetools.keys import hashkey
 logger = logging.getLogger(__name__)
 DISTANCE_THRESHOLD = 0.65
 class CdssService:
-    _cache = TTLCache(maxsize=100000, ttl=60*60*24*30)
+    _cache = TTLCache(maxsize=1000, ttl=60*60*24*30)
 
     kgPropService = KGPropService(next(get_db()))
     def get_disease_detail(self, diease_name: str,category: str):

+ 13 - 4
service/kg_edge_service.py

@@ -14,9 +14,14 @@ class KGEdgeService:
     def __init__(self, db: Session):
         self.db = db
 
-    _cache = TTLCache(maxsize=100000, ttl=60*60*24*30)
+    _cache = TTLCache(maxsize=1000, ttl=60*60*24*30)
     def get_edge(self, edge_id: int):
-        edge = self.db.query(KGEdge).get(edge_id)
+        try:
+            edge = self.db.query(KGEdge).get(edge_id)
+            self.db.commit()
+        except Exception as e:
+            self.db.rollback()
+            raise e
         if not edge:
             raise ValueError("Edge not found")
         return edge
@@ -87,8 +92,12 @@ class KGEdgeService:
                 filters.append(KGEdge.dest_id == dest_id)
             if category is not None:
                 filters.append(KGEdge.category == category)
-            edges = self.db.query(KGEdge).filter(*filters).all()
-
+            try:
+                edges = self.db.query(KGEdge).filter(*filters).all()
+                self.db.commit()
+            except Exception as e:
+                self.db.rollback()
+                raise e
             from service.kg_node_service import KGNodeService
             node_service = KGNodeService(self.db)
             result = []

+ 1 - 1
service/kg_node_service.py

@@ -18,7 +18,7 @@ class KGNodeService:
     def __init__(self, db: Session):
         self.db = db
 
-    _cache = TTLCache(maxsize=100000, ttl=60*60*24*30)
+    _cache = TTLCache(maxsize=1000, ttl=60*60*24*30)
 
     def search_title_index(self, index: str, keywrod: str,category: str, top_k: int = 3,distance: float = 0.3) -> Optional[int]:
         cache_key = f"search_title_index_{index}:{keywrod}:{category}:{top_k}:{distance}"