diff --git a/CONSTITUTION.md b/CONSTITUTION.md index 6dc3068..3b26577 100644 --- a/CONSTITUTION.md +++ b/CONSTITUTION.md @@ -2,7 +2,7 @@ > 本文件定義專案開發的核心準則與不可違反的規範 > **建立日期**: 2026-01-12 -> **當前版本**: V10.54 (Vendor management query service extraction) +> **當前版本**: V10.55 (Cache dashboard competitor decision overview) > **最後更新**: 2026-05-01 --- diff --git a/app.py b/app.py index a4ce81d..0535b0a 100644 --- a/app.py +++ b/app.py @@ -95,8 +95,8 @@ except Exception as e: sys_log.error(f"無法檢測磁碟空間: {e}") # 🚩 系統版本定義 (備份與顯示用) -# 🚩 2026-05-01 V10.54: Vendor management query service extraction -SYSTEM_VERSION = "V10.54" +# 🚩 2026-05-01 V10.55: Cache dashboard competitor decision overview +SYSTEM_VERSION = "V10.55" # ========================================== # 🔒 SQL Injection 防護函數 diff --git a/config.py b/config.py index 6624dfa..48e8fe5 100644 --- a/config.py +++ b/config.py @@ -254,7 +254,7 @@ YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY', '') # ========================================== # 系統版本與路徑 # ========================================== -SYSTEM_VERSION = "V10.54" +SYSTEM_VERSION = "V10.55" LOG_FILE_PATH = os.path.join(BASE_DIR, 'logs/system.log') public_url = PUBLIC_URL # 用於模板顯示 diff --git a/routes/dashboard_routes.py b/routes/dashboard_routes.py index 2afc5eb..5324cf2 100644 --- a/routes/dashboard_routes.py +++ b/routes/dashboard_routes.py @@ -172,6 +172,15 @@ def _dashboard_decision_row(row, tone): def _load_competitor_decision_overview(session): """讀取商品看板第一屏使用的 PChome 比價決策摘要。全部來自正式 DB。""" + cache_key = 'competitor_decision_overview' + cache_ts_key = 'competitor_decision_overview_timestamp' + cached = _DASHBOARD_DATA_CACHE.get(cache_key) + cached_ts = _DASHBOARD_DATA_CACHE.get(cache_ts_key) + if cached and cached_ts: + age = time.time() - cached_ts + if age < min(_DASHBOARD_CACHE_TTL, 300): + return cached + default = { 'total_active': 0, 'matched_count': 0, @@ -369,6 +378,8 @@ def _load_competitor_decision_overview(session): } for row in session.execute(pending_sql).mappings().all() ] + _DASHBOARD_DATA_CACHE[cache_key] = overview + _DASHBOARD_DATA_CACHE[cache_ts_key] = time.time() return overview except Exception as exc: sys_log.warning(f"[Dashboard] PChome 比價決策摘要讀取略過: {exc}") @@ -376,6 +387,8 @@ def _load_competitor_decision_overview(session): session.rollback() except Exception: pass + _DASHBOARD_DATA_CACHE[cache_key] = default + _DASHBOARD_DATA_CACHE[cache_ts_key] = time.time() return default