refactor(stockSlice): 移除 LocalStorage 缓存层,简化为两级缓存架构
This commit is contained in:
@@ -2,35 +2,19 @@
|
|||||||
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
||||||
import { eventService, stockService } from '../../services/eventService';
|
import { eventService, stockService } from '../../services/eventService';
|
||||||
import { logger } from '../../utils/logger';
|
import { logger } from '../../utils/logger';
|
||||||
import { localCacheManager, CACHE_EXPIRY_STRATEGY } from '../../utils/CacheManager';
|
|
||||||
import { getApiBase } from '../../utils/apiConfig';
|
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 ====================
|
// ==================== Async Thunks ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取事件相关股票(三级缓存)
|
* 获取事件相关股票(Redux 缓存)
|
||||||
*/
|
*/
|
||||||
export const fetchEventStocks = createAsyncThunk(
|
export const fetchEventStocks = createAsyncThunk(
|
||||||
'stock/fetchEventStocks',
|
'stock/fetchEventStocks',
|
||||||
async ({ eventId, forceRefresh = false }, { getState }) => {
|
async ({ eventId, forceRefresh = false }, { getState }) => {
|
||||||
logger.debug('stockSlice', 'fetchEventStocks', { eventId, forceRefresh });
|
logger.debug('stockSlice', 'fetchEventStocks', { eventId, forceRefresh });
|
||||||
|
|
||||||
// 1. Redux 状态缓存
|
// Redux 状态缓存
|
||||||
if (!forceRefresh) {
|
if (!forceRefresh) {
|
||||||
const cached = getState().stock.eventStocksCache[eventId];
|
const cached = getState().stock.eventStocksCache[eventId];
|
||||||
if (cached && cached.length > 0) {
|
if (cached && cached.length > 0) {
|
||||||
@@ -39,27 +23,13 @@ export const fetchEventStocks = createAsyncThunk(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. LocalStorage 缓存
|
// API 请求
|
||||||
if (!forceRefresh) {
|
|
||||||
const localCached = localCacheManager.get(CACHE_KEYS.EVENT_STOCKS + eventId);
|
|
||||||
if (localCached) {
|
|
||||||
logger.debug('stockSlice', 'LocalStorage 缓存命中', { eventId });
|
|
||||||
return { eventId, stocks: localCached };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. API 请求
|
|
||||||
const res = await eventService.getRelatedStocks(eventId);
|
const res = await eventService.getRelatedStocks(eventId);
|
||||||
if (res.success && res.data) {
|
if (res.success && res.data) {
|
||||||
logger.debug('stockSlice', 'API 请求成功', {
|
logger.debug('stockSlice', 'API 请求成功', {
|
||||||
eventId,
|
eventId,
|
||||||
stockCount: res.data.length
|
stockCount: res.data.length
|
||||||
});
|
});
|
||||||
localCacheManager.set(
|
|
||||||
CACHE_KEYS.EVENT_STOCKS + eventId,
|
|
||||||
res.data,
|
|
||||||
CACHE_EXPIRY_STRATEGY.LONG // 1小时
|
|
||||||
);
|
|
||||||
return { eventId, stocks: res.data };
|
return { eventId, stocks: res.data };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +54,7 @@ export const fetchStockQuotes = createAsyncThunk(
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取事件详情
|
* 获取事件详情(Redux 缓存)
|
||||||
*/
|
*/
|
||||||
export const fetchEventDetail = createAsyncThunk(
|
export const fetchEventDetail = createAsyncThunk(
|
||||||
'stock/fetchEventDetail',
|
'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 请求
|
// API 请求
|
||||||
const res = await eventService.getEventDetail(eventId);
|
const res = await eventService.getEventDetail(eventId);
|
||||||
if (res.success && res.data) {
|
if (res.success && res.data) {
|
||||||
localCacheManager.set(
|
|
||||||
CACHE_KEYS.EVENT_DETAIL + eventId,
|
|
||||||
res.data,
|
|
||||||
CACHE_EXPIRY_STRATEGY.LONG
|
|
||||||
);
|
|
||||||
return { eventId, detail: res.data };
|
return { eventId, detail: res.data };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +81,7 @@ export const fetchEventDetail = createAsyncThunk(
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取历史事件对比
|
* 获取历史事件对比(Redux 缓存)
|
||||||
*/
|
*/
|
||||||
export const fetchHistoricalEvents = createAsyncThunk(
|
export const fetchHistoricalEvents = createAsyncThunk(
|
||||||
'stock/fetchHistoricalEvents',
|
'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 请求
|
// API 请求
|
||||||
const res = await eventService.getHistoricalEvents(eventId);
|
const res = await eventService.getHistoricalEvents(eventId);
|
||||||
if (res.success && res.data) {
|
if (res.success && res.data) {
|
||||||
localCacheManager.set(
|
|
||||||
CACHE_KEYS.HISTORICAL_EVENTS + eventId,
|
|
||||||
res.data,
|
|
||||||
CACHE_EXPIRY_STRATEGY.LONG
|
|
||||||
);
|
|
||||||
return { eventId, events: res.data };
|
return { eventId, events: res.data };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +107,7 @@ export const fetchHistoricalEvents = createAsyncThunk(
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取传导链分析
|
* 获取传导链分析(Redux 缓存)
|
||||||
*/
|
*/
|
||||||
export const fetchChainAnalysis = createAsyncThunk(
|
export const fetchChainAnalysis = createAsyncThunk(
|
||||||
'stock/fetchChainAnalysis',
|
'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 请求
|
// API 请求
|
||||||
const res = await eventService.getTransmissionChainAnalysis(eventId);
|
const res = await eventService.getTransmissionChainAnalysis(eventId);
|
||||||
if (res.success && res.data) {
|
if (res.success && res.data) {
|
||||||
localCacheManager.set(
|
|
||||||
CACHE_KEYS.CHAIN_ANALYSIS + eventId,
|
|
||||||
res.data,
|
|
||||||
CACHE_EXPIRY_STRATEGY.LONG
|
|
||||||
);
|
|
||||||
return { eventId, analysis: res.data };
|
return { eventId, analysis: res.data };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user