From 05ea9e70452fd5c211c29af8f8b378cb4cabfadf Mon Sep 17 00:00:00 2001 From: DingZQ Date: Sun, 9 Mar 2025 00:23:27 +0800 Subject: [PATCH] Cache data in fastapi side --- influxdb_api.py | 18 ------------------ main.py | 22 ++++++++++++++++++++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/influxdb_api.py b/influxdb_api.py index bb29175..d152150 100644 --- a/influxdb_api.py +++ b/influxdb_api.py @@ -1406,12 +1406,6 @@ def query_all_SCADA_records_by_date(query_date: str, bucket: str="SCADA_data", c :return: """ - global influxdb_cache - - cache_key = f"{query_date}" - if influxdb_cache.get(cache_key) is not None: - return influxdb_cache.get(cache_key) - if client.ping(): print("{} -- Successfully connected to InfluxDB.".format( datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) else: print("{} -- Failed to connect to InfluxDB.".format( datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) @@ -1457,8 +1451,6 @@ def query_all_SCADA_records_by_date(query_date: str, bucket: str="SCADA_data", c except Exception as e: print(f"Error querying InfluxDB for date {query_date}: {e}") - influxdb_cache[cache_key] = SCADA_results - return SCADA_results @@ -1842,8 +1834,6 @@ def query_all_record_by_date(query_date: str, bucket: str="realtime_simulation_r }) return node_records, link_records -influxdb_cache = {} - # 2025/02/21 WMH def query_all_record_by_date_property(query_date: str, type: str, property: str, bucket: str="realtime_simulation_result", client: InfluxDBClient=client) -> list: @@ -1857,12 +1847,6 @@ def query_all_record_by_date_property(query_date: str, type: str, property: str, :return: list(dict): result_records """ - global influxdb_cache - - cache_key = f"{query_date}_{type}_{property}" - if influxdb_cache.get(cache_key) is not None: - return influxdb_cache.get(cache_key) - if client.ping(): print("{} -- Successfully connected to InfluxDB.".format( datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) else: print("{} -- Failed to connect to InfluxDB.".format( datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) @@ -1913,8 +1897,6 @@ def query_all_record_by_date_property(query_date: str, type: str, property: str, property: record["_value"] }) - influxdb_cache[cache_key] = result_records - return result_records diff --git a/main.py b/main.py index 7b3fff1..9b3e7c8 100644 --- a/main.py +++ b/main.py @@ -48,6 +48,8 @@ tmpDir = "C:/tmpfiles/" lockedPrjs = {} +# 缓存 influxdb 查询结果提高性能 +influxdb_cache = {} if not os.path.exists(inpDir): os.mkdir(inpDir) @@ -2149,8 +2151,16 @@ async def fastapi_query_all_records_by_date(querydate: str) -> dict[str, list]: # 返回 [{'time': '2024-01-01T00:00:00Z', 'ID': '1', 'value': 1.0}, {'time': '2024-01-01T00:00:00Z', 'ID': '2', 'value': 2.0}] @app.get("/queryallrecordsbydateproperty/") async def fastapi_query_all_records_by_date_property(querydate: str, querytype: str, property: str) -> list[dict]: - return influxdb_api.query_all_record_by_date_property(query_date=querydate, type=querytype, property=property, client=influx_client) + # 缓存查询结果提高性能 + global influxdb_cache + cache_key = f"{querydate}_{querytype}_{property}" + if influxdb_cache.get(cache_key) is not None: + return influxdb_cache.get(cache_key) + result = influxdb_api.query_all_record_by_date_property(query_date=querydate, type=querytype, property=property, client=influx_client) + influxdb_cache[cache_key] = result + + return result # def query_curve_by_ID_property_daterange(ID: str, type: str, property: str, start_date: str, end_date: str, bucket: str="realtime_data", client: InfluxDBClient=client) -> list: @@ -2185,8 +2195,16 @@ async def fastapi_query_scada_data_by_device_id_and_date(ids: str, querydate: st # 返回所有SCADA设备在指定日期的所有记录 @app.get("/queryallscadarecordsbydate/") async def fastapi_query_all_scada_records_by_date(querydate: str): - return influxdb_api.query_all_SCADA_records_by_date(query_date=querydate, client=influx_client) + # 缓存查询结果提高性能 + global influxdb_cache + cache_key = f"{querydate}" + if influxdb_cache.get(cache_key) is not None: + return influxdb_cache.get(cache_key) + result = influxdb_api.query_all_SCADA_records_by_date(query_date=querydate, client=influx_client) + influxdb_cache[cache_key] = result + + return result @app.get("/queryinfluxdbbuckets/")