From 39bb19363ebeffb845010c3ae62897c2c093572f Mon Sep 17 00:00:00 2001 From: DingZQ Date: Sat, 29 Mar 2025 21:32:54 +0800 Subject: [PATCH] Cache methods getall***properties --- main.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 80a0598..27cdda7 100644 --- a/main.py +++ b/main.py @@ -652,8 +652,19 @@ async def fastapi_get_junction_properties(network: str, junction: str) -> dict[s # DingZQ, 2025-03-29 @app.get("/getalljunctionproperties/") async def fastapi_get_all_junction_properties(network: str) -> list[dict[str, Any]]: - return get_all_junctions(network) + # 缓存查询结果提高性能 + global redis_client + cache_key = f"getalljunctionproperties_{network}" + data = redis_client.get(cache_key) + if data: + # 使用自定义的反序列化函数 + loaded_dict = msgpack.unpackb(data, object_hook=object_hook) + return loaded_dict + results = get_all_junctions(network) + redis_client.set(cache_key, msgpack.packb(results, default=object_hook)) + + return results @app.post("/setjunctionproperties/",response_model=None) @@ -748,7 +759,20 @@ async def fastapi_get_reservoir_properties(network: str, reservoir: str) -> dict # DingZQ, 2025-03-29 @app.get("/getallreservoirproperties/") async def fastapi_get_all_reservoir_properties(network: str) -> list[dict[str, Any]]: - return get_all_reservoirs(network) + # 缓存查询结果提高性能 + global redis_client + cache_key = f"getallreservoirproperties_{network}" + data = redis_client.get(cache_key) + if data: + # 使用自定义的反序列化函数 + loaded_dict = msgpack.unpackb(data, object_hook=object_hook) + return loaded_dict + + results = get_all_reservoirs(network) + redis_client.set(cache_key, msgpack.packb(results, default=object_hook)) + + return results + @app.post("/setreservoirproperties/",response_model=None) async def fastapi_set_reservoir_properties(network: str, reservoir: str @@ -915,7 +939,19 @@ async def fastapi_get_tank_properties(network: str, tank: str) -> dict[str, Any] # DingZQ, 2025-03-29 @app.get("/getalltankproperties/") async def fastapi_get_all_tank_properties(network: str) -> list[dict[str, Any]]: - return get_all_tanks(network) + # 缓存查询结果提高性能 + global redis_client + cache_key = f"getalltankproperties_{network}" + data = redis_client.get(cache_key) + if data: + # 使用自定义的反序列化函数 + loaded_dict = msgpack.unpackb(data, object_hook=object_hook) + return loaded_dict + + results = get_all_tanks(network) + redis_client.set(cache_key, msgpack.packb(results, default=object_hook)) + + return results @app.post("/settankproperties/",response_model=None) async def fastapi_set_tank_properties(network: str, tank: str, req: Request) -> ChangeSet: @@ -1038,7 +1074,20 @@ async def fastapi_get_pipe_properties(network: str, pipe: str) -> dict[str, Any] # DingZQ, 2025-03-29 @app.get('/getallpipeproperties/') async def fastapi_get_all_pipe_properties(network: str) -> list[dict[str, Any]]: - return get_all_pipes(network) + # 缓存查询结果提高性能 + global redis_client + cache_key = f"getallpipeproperties_{network}" + data = redis_client.get(cache_key) + if data: + # 使用自定义的反序列化函数 + loaded_dict = msgpack.unpackb(data, object_hook=object_hook) + return loaded_dict + + results = get_all_pipes(network) + redis_client.set(cache_key, msgpack.packb(results, default=object_hook)) + + return results + @app.post("/setpipeproperties/",response_model=None) async def fastapi_set_pipe_properties(network: str, pipe: str, req: Request) -> ChangeSet: @@ -1097,7 +1146,19 @@ async def fastapi_get_pump_properties(network: str, pump: str) -> dict[str, Any] # DingZQ, 2025-03-29 @app.get('/getallpumpproperties/') async def fastapi_get_all_pump_properties(network: str) -> list[dict[str, Any]]: - return get_all_pumps(network) + # 缓存查询结果提高性能 + global redis_client + cache_key = f"getallpumpproperties_{network}" + data = redis_client.get(cache_key) + if data: + # 使用自定义的反序列化函数 + loaded_dict = msgpack.unpackb(data, object_hook=object_hook) + return loaded_dict + + results = get_all_pumps(network) + redis_client.set(cache_key, msgpack.packb(results, default=object_hook)) + + return results @app.post("/setpumpproperties/",response_model=None) async def fastapi_set_pump_properties(network: str, pump: str, req: Request) -> ChangeSet: @@ -1197,7 +1258,19 @@ async def fastapi_get_valve_properties(network: str, valve: str) -> dict[str, An # DingZQ, 2025-03-29 @app.get('/getallvalveproperties/') async def fastapi_get_all_valve_properties(network: str) -> list[dict[str, Any]]: - return get_all_valves(network) + # 缓存查询结果提高性能 + global redis_client + cache_key = f"getallvalveproperties_{network}" + data = redis_client.get(cache_key) + if data: + # 使用自定义的反序列化函数 + loaded_dict = msgpack.unpackb(data, object_hook=object_hook) + return loaded_dict + + results = get_all_valves(network) + redis_client.set(cache_key, msgpack.packb(results, default=object_hook)) + + return results @app.post("/setvalveproperties/",response_model=None) async def fastapi_set_valve_properties(network: str, valve: str, req: Request) -> ChangeSet: