From e56e3e0078faa3a2bba89817ce2487e6cd2cbb5d Mon Sep 17 00:00:00 2001 From: DingZQ Date: Wed, 19 Mar 2025 22:21:53 +0800 Subject: [PATCH] Refine --- auto_cache.py | 26 +++++++++++++++++++++++++- main.py | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/auto_cache.py b/auto_cache.py index ee09982..a137c94 100644 --- a/auto_cache.py +++ b/auto_cache.py @@ -1,10 +1,34 @@ import schedule import time +import datetime import shutil +import redis +import urllib.request + +def query_all_records_by_date_with_type_flow(date: str): + try: + response = urllib.request.urlopen('localhost/queryallrecordbydatewithtype/?querydate={date}&querytype=flow') + html = response.read().decode('utf-8') + print(html) + except urllib.error.URLError as e: + print("Error") def auto_cache_data(): - + redis_client = redis.redis(host='localhost', port=6379, db=0) + + items = ['queryallrecordsbydatewithtype_[DATE]_flow'] + + today - datetime.date.today() + print(today) + for item in items: + prev_day = today.deltaday(-1) + str_prev_day = prev_day.strftime('%Y-%m-%d') + print(str_prev_day) + key = item.replace('[DATE]', str_prev_day) + + if not redis_client.exists(key): + query_all_records_by_date_with_type_flow(str_prev_day) if __name__ == "__main__": auto_cache_data() diff --git a/main.py b/main.py index b1f2d51..3b89db2 100644 --- a/main.py +++ b/main.py @@ -2245,7 +2245,7 @@ async def fastapi_query_scada_data_by_device_id_and_date(ids: str, querydate: st async def fastapi_query_all_scada_records_by_date(querydate: str): # 缓存查询结果提高性能 global redis_client - cache_key = f"{querydate}" + cache_key = f"queryallscadarecordsbydate_{querydate}" data = redis_client.get(cache_key) if data: # 使用自定义的反序列化函数