Cache getnetworkgeometries

This commit is contained in:
DingZQ
2025-03-29 21:24:01 +08:00
parent 90eb873ecf
commit 08586fc012

29
main.py
View File

@@ -1611,6 +1611,15 @@ async def fastapi_get_node_coord(network: str, node: str) -> dict[str, float] |
# link type: pipe, pump, valve
@app.get("/getnetworkgeometries/")
async def fastapi_get_network_geometries(network: str) -> dict[str, Any] | None:
# 获取所有节点坐标# 缓存查询结果提高性能
global redis_client
cache_key = f"getnetworkgeometries_{network}"
data = redis_client.get(cache_key)
if data:
# 使用自定义的反序列化函数
loaded_dict = msgpack.unpackb(data, object_hook=object_hook)
return loaded_dict
coords = get_network_node_coords(network)
nodes = []
for node_id, coord in coords.items():
@@ -1623,9 +1632,14 @@ async def fastapi_get_network_geometries(network: str) -> dict[str, Any] | None:
# data from WMH's scada info
scadas = get_all_scada_info(network)
return { 'nodes': nodes,
'links': links,
'scadas': scadas }
results = { 'nodes': nodes,
'links': links,
'scadas': scadas }
# 缓存查询结果提高性能
redis_client.set(cache_key, msgpack.packb(results, default=object_hook))
return results
# DingZQ, 2024-12-31, get major node coord
# id:type:x:y
@@ -2237,8 +2251,12 @@ async def fastapi_query_all_records_by_date(querydate: str) -> dict[str, list]:
return loaded_dict
nodes_links: tuple = influxdb_api.query_all_record_by_date(query_date=querydate)
return { "nodes": nodes_links[0],
"links": nodes_links[1] }
results = { "nodes": nodes_links[0],
"links": nodes_links[1] }
redis_client.set(cache_key, msgpack.packb(results, default=object_hook))
return results
#2025-03-15, DingZQ
@app.get("/queryallrecordsbydatewithtype/")
@@ -2253,6 +2271,7 @@ async def fastapi_query_all_records_by_date_with_type(querydate: str, querytype:
return loaded_dict
results = influxdb_api.query_all_records_by_date_with_type(query_date=querydate, query_type=querytype)
packed = msgpack.packb(results, default=default_encoder)
redis_client.set(cache_key, packed)