Browse Source

增加缓存和排序

yuchengwei 1 month ago
parent
commit
b136bb4199
1 changed files with 15 additions and 3 deletions
  1. 15 3
      router/text_search.py

+ 15 - 3
router/text_search.py

@@ -21,6 +21,9 @@ from cachetools import TTLCache
 logger = logging.getLogger(__name__)
 router = APIRouter(prefix="/text", tags=["Text Search"])
 
+# 创建全局缓存实例
+cache = TTLCache(maxsize=1000, ttl=3600)
+
 class TextSearchRequest(BaseModel):
     text: str
     conversation_id: Optional[str] = None
@@ -63,6 +66,16 @@ class NodePropsSearchRequest(BaseModel):
     node_id: int
     props_ids: List[int]
 
+@router.post("/clear_cache", response_model=StandardResponse)
+async def clear_cache():
+    try:
+        # 清除全局缓存
+        cache.clear()
+        return StandardResponse(success=True, data={"message": "缓存已清除"})
+    except Exception as e:
+        logger.error(f"清除缓存失败: {str(e)}")
+        raise HTTPException(status_code=500, detail=str(e))
+
 @router.post("/search", response_model=StandardResponse)
 async def search_text(request: TextSearchRequest):
     try:
@@ -316,7 +329,6 @@ async def compare_text(request: TextCompareMultiRequest):
         logger.info(f"mr_match接口耗时: {(end_time - start_time) * 1000:.2f}ms")
         raise HTTPException(status_code=500, detail=str(e))
 
-cache = TTLCache(maxsize=1000, ttl=3600)
 @router.post("/eb_search", response_model=StandardResponse)
 async def node_props_search(request: NodePropsSearchRequest, db: Session = Depends(get_db)):
     try:
@@ -552,11 +564,11 @@ async def node_props_search(request: NodePropsSearchRequest, db: Session = Depen
                                 break
         
         # 将文件信息添加到结果中
-        result["files"] = [{
+        result["files"] = sorted([{
             "file_name": file_name,
             "file_type": file_type,
             "index": str(file_index_map[file_name])
-        } for file_name, file_type in all_files]
+        } for file_name, file_type in all_files], key=lambda x: int(x["index"]))
         
         end_time = time.time()
         logger.info(f"node_props_search接口耗时: {(end_time - start_time) * 1000:.2f}ms")