From f5c46ae71bf20d86d84b51f119c31f3bb58b2066 Mon Sep 17 00:00:00 2001 From: zzlgreat Date: Mon, 15 Dec 2025 18:01:52 +0800 Subject: [PATCH] update pay ui --- src/services/ztStaticService.js | 53 +++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/services/ztStaticService.js b/src/services/ztStaticService.js index 294a233e..177a1824 100644 --- a/src/services/ztStaticService.js +++ b/src/services/ztStaticService.js @@ -7,24 +7,44 @@ // 数据基础路径 const DATA_BASE_URL = '/data/zt'; -// 内存缓存 +// 缓存过期时间(毫秒)- dates.json 缓存5分钟,daily数据缓存30分钟 +const CACHE_TTL = { + dates: 5 * 60 * 1000, // 5分钟 + daily: 30 * 60 * 1000, // 30分钟 + stocksJsonl: 60 * 60 * 1000, // 1小时 +}; + +// 内存缓存(带过期时间) const cache = { dates: null, + datesTimestamp: 0, daily: new Map(), - stocksJsonl: null, // 缓存 stocks.jsonl 数据 + dailyTimestamps: new Map(), + stocksJsonl: null, + stocksJsonlTimestamp: 0, +}; + +/** + * 检查缓存是否过期 + */ +const isCacheExpired = (timestamp, ttl) => { + return Date.now() - timestamp > ttl; }; /** * 获取可用日期列表 + * @param {boolean} forceRefresh - 是否强制刷新缓存 */ -export const fetchAvailableDates = async () => { +export const fetchAvailableDates = async (forceRefresh = false) => { try { - // 使用缓存 - if (cache.dates) { - return { success: true, events: cache.dates }; + // 使用缓存(未过期且非强制刷新) + if (!forceRefresh && cache.dates && !isCacheExpired(cache.datesTimestamp, CACHE_TTL.dates)) { + console.log('[ztStaticService] fetchAvailableDates: using cache'); + return { success: true, events: cache.dates, from_cache: true }; } - const response = await fetch(`${DATA_BASE_URL}/dates.json`); + console.log('[ztStaticService] fetchAvailableDates: fetching from server'); + const response = await fetch(`${DATA_BASE_URL}/dates.json?t=${Date.now()}`); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } @@ -44,8 +64,9 @@ export const fetchAvailableDates = async () => { // 缓存结果 cache.dates = events; + cache.datesTimestamp = Date.now(); - return { success: true, events, total: events.length }; + return { success: true, events, total: events.length, from_cache: false }; } catch (error) { console.error('[ztStaticService] fetchAvailableDates error:', error); return { success: false, error: error.message, events: [] }; @@ -54,15 +75,18 @@ export const fetchAvailableDates = async () => { /** * 获取指定日期的分析数据 + * @param {string} date - 日期(YYYYMMDD格式) + * @param {boolean} forceRefresh - 是否强制刷新缓存 */ -export const fetchDailyAnalysis = async (date) => { +export const fetchDailyAnalysis = async (date, forceRefresh = false) => { try { - // 使用缓存 - if (cache.daily.has(date)) { + // 使用缓存(未过期且非强制刷新) + const cachedTimestamp = cache.dailyTimestamps.get(date); + if (!forceRefresh && cache.daily.has(date) && cachedTimestamp && !isCacheExpired(cachedTimestamp, CACHE_TTL.daily)) { return { success: true, data: cache.daily.get(date), from_cache: true }; } - const response = await fetch(`${DATA_BASE_URL}/daily/${date}.json`); + const response = await fetch(`${DATA_BASE_URL}/daily/${date}.json?t=${Date.now()}`); if (!response.ok) { if (response.status === 404) { return { success: false, error: `日期 ${date} 的数据不存在` }; @@ -102,6 +126,7 @@ export const fetchDailyAnalysis = async (date) => { // 缓存结果 cache.daily.set(date, data); + cache.dailyTimestamps.set(date, Date.now()); return { success: true, data, from_cache: false }; } catch (error) { @@ -348,8 +373,12 @@ export const fetchStocksBatchDetail = async (codes, date) => { */ export const clearCache = () => { cache.dates = null; + cache.datesTimestamp = 0; cache.daily.clear(); + cache.dailyTimestamps.clear(); cache.stocksJsonl = null; + cache.stocksJsonlTimestamp = 0; + console.log('[ztStaticService] Cache cleared'); }; export default {