import { RequestCacheService, RequestAdapter } from '@jolibox/common'; import { IHttpClient } from '@/common/http'; import { StandardResponse } from '@jolibox/types'; interface BalanceRequest { endpoint: string; query?: Record; method?: 'GET' | 'POST'; } // 定义余额响应类型 interface BalanceResponse { balance: number; } interface BalanceCacheData { balance: number; timestamp: number; } class BalanceRequestAdapter implements RequestAdapter, BalanceCacheData, undefined> { constructor(private httpClient: IHttpClient) {} async execute(endpoint: string, options?: BalanceRequest): Promise> { const method = options?.method || 'GET'; const query = options?.query || {}; if (method === 'GET') { return await this.httpClient.get>(endpoint, { query: query as Record }); } else { return await this.httpClient.post>(endpoint, { query: query as Record }); } } extractCacheableData(response: StandardResponse): BalanceCacheData { return { balance: response.data?.balance || 0, timestamp: Date.now() }; } // 余额使用纯缓存模式 extractRealTimeData?: undefined; mergeData?: undefined; processCachedData(cached: BalanceCacheData): StandardResponse { return { code: 'SUCCESS', message: 'success from cache', data: { balance: cached.balance } }; } } // 缓存余额服务 export class CachedBalanceService { private balanceService: RequestCacheService< BalanceRequest, StandardResponse, BalanceCacheData, undefined >; constructor(httpClient: IHttpClient) { this.balanceService = new (RequestCacheService as new (...args: unknown[]) => RequestCacheService< BalanceRequest, StandardResponse, BalanceCacheData, undefined >)(new BalanceRequestAdapter(httpClient), { duration: 20 * 60 * 1000, // 10min cache, force update every query timeout: 1000, // 1s timeout for request keyGenerator: (endpoint: string, params?: BalanceRequest) => { const queryStr = params?.query ? JSON.stringify(params.query) : ''; return `${endpoint}:${queryStr}`; } }); } async getBalance(type = 'JOLI_COIN'): Promise { const response = await this.balanceService.request('/api/joli-coin/balance', { endpoint: '/api/joli-coin/balance', query: { type }, method: 'GET' }); return response.data; } async refreshBalance(type = 'JOLI_COIN'): Promise { const response = await this.balanceService.forceRequest('/api/joli-coin/balance', { endpoint: '/api/joli-coin/balance', query: { type }, method: 'GET' }); return response.data; } async warmupBalance(type = 'JOLI_COIN'): Promise { await this.balanceService.warmupCache('/api/joli-coin/balance', { endpoint: '/api/joli-coin/balance', query: { type }, method: 'GET' }); } clearBalanceCache(): void { this.balanceService.clearCache(); } getBalanceCacheStats() { return this.balanceService.getCacheStats(); } clearExpiredCache(): void { this.balanceService.clearExpiredCache(); } } // create instance let cachedBalanceServiceInstance: CachedBalanceService | null = null; function getCachedBalanceService(httpClient: IHttpClient): CachedBalanceService { if (!cachedBalanceServiceInstance) { cachedBalanceServiceInstance = new CachedBalanceService(httpClient); } return cachedBalanceServiceInstance; } /** * 缓存版本的 fetchBalance 函数 */ export const fetchBalanceCached = async (httpClient: IHttpClient, type = 'JOLI_COIN') => { const cachedBalanceService = getCachedBalanceService(httpClient); return await cachedBalanceService.getBalance(type); }; /** * 强制刷新余额缓存 */ export const refreshBalanceCache = async (httpClient: IHttpClient, type = 'JOLI_COIN') => { const cachedBalanceService = getCachedBalanceService(httpClient); const result = await cachedBalanceService.refreshBalance(type); console.log(`Balance cache refreshed for type: ${type}`); return result; }; /** * 预热余额缓存 */ export const warmupBalanceCache = async (httpClient: IHttpClient, type = 'JOLI_COIN') => { const cachedBalanceService = getCachedBalanceService(httpClient); await cachedBalanceService.warmupBalance(type); console.log(`Balance cache warmed up for type: ${type}`); }; export const clearBalanceCache = (httpClient: IHttpClient) => { const cachedBalanceService = getCachedBalanceService(httpClient); cachedBalanceService.clearBalanceCache(); console.log('Balance cache cleared'); }; export const getBalanceCacheStats = (httpClient: IHttpClient) => { const cachedBalanceService = getCachedBalanceService(httpClient); return cachedBalanceService.getBalanceCacheStats(); }; export const getBalanceCacheService = (httpClient: IHttpClient): CachedBalanceService => { return getCachedBalanceService(httpClient); };