From 39a2ccd53b20f09fc2b5422f66736f49940e16ff Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Mon, 3 Nov 2025 11:58:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor(stockSlice):=20=E7=A7=BB=E9=99=A4=20Lo?= =?UTF-8?q?calStorage=20=E7=BC=93=E5=AD=98=E5=B1=82=EF=BC=8C=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E4=B8=BA=E4=B8=A4=E7=BA=A7=E7=BC=93=E5=AD=98=E6=9E=B6?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/slices/stockSlice.js | 82 +++------------------------------- 1 file changed, 6 insertions(+), 76 deletions(-) diff --git a/src/store/slices/stockSlice.js b/src/store/slices/stockSlice.js index a33185ba..0b6982f4 100644 --- a/src/store/slices/stockSlice.js +++ b/src/store/slices/stockSlice.js @@ -2,35 +2,19 @@ import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; import { eventService, stockService } from '../../services/eventService'; import { logger } from '../../utils/logger'; -import { localCacheManager, CACHE_EXPIRY_STRATEGY } from '../../utils/CacheManager'; import { getApiBase } from '../../utils/apiConfig'; -// ==================== 常量定义 ==================== - -// 缓存键名 -const CACHE_KEYS = { - EVENT_STOCKS: 'event_stocks_', - EVENT_DETAIL: 'event_detail_', - HISTORICAL_EVENTS: 'historical_events_', - CHAIN_ANALYSIS: 'chain_analysis_', - EXPECTATION_SCORE: 'expectation_score_', - WATCHLIST: 'user_watchlist' -}; - -// 请求去重:缓存正在进行的请求 -const pendingRequests = new Map(); - // ==================== Async Thunks ==================== /** - * 获取事件相关股票(三级缓存) + * 获取事件相关股票(Redux 缓存) */ export const fetchEventStocks = createAsyncThunk( 'stock/fetchEventStocks', async ({ eventId, forceRefresh = false }, { getState }) => { logger.debug('stockSlice', 'fetchEventStocks', { eventId, forceRefresh }); - // 1. Redux 状态缓存 + // Redux 状态缓存 if (!forceRefresh) { const cached = getState().stock.eventStocksCache[eventId]; if (cached && cached.length > 0) { @@ -39,27 +23,13 @@ export const fetchEventStocks = createAsyncThunk( } } - // 2. LocalStorage 缓存 - if (!forceRefresh) { - const localCached = localCacheManager.get(CACHE_KEYS.EVENT_STOCKS + eventId); - if (localCached) { - logger.debug('stockSlice', 'LocalStorage 缓存命中', { eventId }); - return { eventId, stocks: localCached }; - } - } - - // 3. API 请求 + // API 请求 const res = await eventService.getRelatedStocks(eventId); if (res.success && res.data) { logger.debug('stockSlice', 'API 请求成功', { eventId, stockCount: res.data.length }); - localCacheManager.set( - CACHE_KEYS.EVENT_STOCKS + eventId, - res.data, - CACHE_EXPIRY_STRATEGY.LONG // 1小时 - ); return { eventId, stocks: res.data }; } @@ -84,7 +54,7 @@ export const fetchStockQuotes = createAsyncThunk( ); /** - * 获取事件详情 + * 获取事件详情(Redux 缓存) */ export const fetchEventDetail = createAsyncThunk( 'stock/fetchEventDetail', @@ -100,23 +70,9 @@ export const fetchEventDetail = createAsyncThunk( } } - // LocalStorage 缓存 - if (!forceRefresh) { - const localCached = localCacheManager.get(CACHE_KEYS.EVENT_DETAIL + eventId); - if (localCached) { - logger.debug('stockSlice', 'LocalStorage 缓存命中 - eventDetail', { eventId }); - return { eventId, detail: localCached }; - } - } - // API 请求 const res = await eventService.getEventDetail(eventId); if (res.success && res.data) { - localCacheManager.set( - CACHE_KEYS.EVENT_DETAIL + eventId, - res.data, - CACHE_EXPIRY_STRATEGY.LONG - ); return { eventId, detail: res.data }; } @@ -125,7 +81,7 @@ export const fetchEventDetail = createAsyncThunk( ); /** - * 获取历史事件对比 + * 获取历史事件对比(Redux 缓存) */ export const fetchHistoricalEvents = createAsyncThunk( 'stock/fetchHistoricalEvents', @@ -140,22 +96,9 @@ export const fetchHistoricalEvents = createAsyncThunk( } } - // LocalStorage 缓存 - if (!forceRefresh) { - const localCached = localCacheManager.get(CACHE_KEYS.HISTORICAL_EVENTS + eventId); - if (localCached) { - return { eventId, events: localCached }; - } - } - // API 请求 const res = await eventService.getHistoricalEvents(eventId); if (res.success && res.data) { - localCacheManager.set( - CACHE_KEYS.HISTORICAL_EVENTS + eventId, - res.data, - CACHE_EXPIRY_STRATEGY.LONG - ); return { eventId, events: res.data }; } @@ -164,7 +107,7 @@ export const fetchHistoricalEvents = createAsyncThunk( ); /** - * 获取传导链分析 + * 获取传导链分析(Redux 缓存) */ export const fetchChainAnalysis = createAsyncThunk( 'stock/fetchChainAnalysis', @@ -179,22 +122,9 @@ export const fetchChainAnalysis = createAsyncThunk( } } - // LocalStorage 缓存 - if (!forceRefresh) { - const localCached = localCacheManager.get(CACHE_KEYS.CHAIN_ANALYSIS + eventId); - if (localCached) { - return { eventId, analysis: localCached }; - } - } - // API 请求 const res = await eventService.getTransmissionChainAnalysis(eventId); if (res.success && res.data) { - localCacheManager.set( - CACHE_KEYS.CHAIN_ANALYSIS + eventId, - res.data, - CACHE_EXPIRY_STRATEGY.LONG - ); return { eventId, analysis: res.data }; }