refactor(stockSlice): 移除 LocalStorage 缓存层,简化为两级缓存架构

This commit is contained in:
zdl
2025-11-03 11:58:39 +08:00
parent 6160edf060
commit 39a2ccd53b

View File

@@ -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 };
}