From 4ce5a4228c9bac238d6adbdcdceff9d884c7d9a5 Mon Sep 17 00:00:00 2001 From: jeevi cao <542561541@qq.com> Date: Tue, 13 May 2025 14:35:23 +0800 Subject: [PATCH] feat: endpoint basic examples add cache Increase interface speed (#7124) * Update py_autofix.yml * feat: endpoint basic_examples add cache * Revert "Update py_autofix.yml" This reverts commit 7aed45e98602f57c2d6d33532080bad30cf02507. * resolve conflicts * refactor: update response handling in read_basic_examples function --------- Co-authored-by: tianzhipeng Co-authored-by: caojianwei-jk --- src/backend/base/langflow/api/v1/flows.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/backend/base/langflow/api/v1/flows.py b/src/backend/base/langflow/api/v1/flows.py index 5f0335f80..531ae888d 100644 --- a/src/backend/base/langflow/api/v1/flows.py +++ b/src/backend/base/langflow/api/v1/flows.py @@ -11,7 +11,7 @@ from uuid import UUID import orjson from aiofile import async_open from anyio import Path -from fastapi import APIRouter, Depends, File, HTTPException, UploadFile +from fastapi import APIRouter, Depends, File, HTTPException, Response, UploadFile from fastapi.encoders import jsonable_encoder from fastapi.responses import StreamingResponse from fastapi_pagination import Page, Params @@ -526,6 +526,9 @@ async def download_multiple_file( return flows_without_api_keys[0] +all_starter_folder_flows_response: Response | None = None + + @router.get("/basic_examples/", response_model=list[FlowRead], status_code=200) async def read_basic_examples( *, @@ -540,17 +543,24 @@ async def read_basic_examples( list[FlowRead]: A list of basic example flows. """ try: - # Get the starter project + global all_starter_folder_flows_response # noqa: PLW0603 + + if all_starter_folder_flows_response: + return all_starter_folder_flows_response + # Get the starter folder starter_folder = (await session.exec(select(Folder).where(Folder.name == STARTER_FOLDER_NAME))).first() if not starter_folder: return [] - # Get all flows in the starter project - flows = (await session.exec(select(Flow).where(Flow.folder_id == starter_folder.id))).all() + # Get all flows in the starter folder + all_starter_folder_flows = (await session.exec(select(Flow).where(Flow.folder_id == starter_folder.id))).all() + + flow_reads = [FlowRead.model_validate(flow, from_attributes=True) for flow in all_starter_folder_flows] + all_starter_folder_flows_response = compress_response(flow_reads) # Return compressed response using our utility function - return compress_response(flows) + return all_starter_folder_flows_response # noqa: TRY300 except Exception as e: raise HTTPException(status_code=500, detail=str(e)) from e