Files
JKVideo/store/playUrlCache.ts
Developer 53c67079a1 bug修改
2026-05-12 20:27:30 +08:00

44 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { create } from 'zustand';
import type { PlayUrlResponse } from '../services/types';
interface CacheEntry {
playData: PlayUrlResponse;
mpdUri?: string;
ts: number;
}
interface PlayUrlCacheStore {
// 不通过 set 触发渲染(只是用 store 持有 Map
cache: Map<string, CacheEntry>;
get: (bvid: string, qn: number) => CacheEntry | undefined;
set: (bvid: string, qn: number, entry: Omit<CacheEntry, 'ts'>) => void;
}
const TTL_MS = 5 * 60 * 1000;
const MAX_ENTRIES = 30;
const makeKey = (bvid: string, qn: number) => `${bvid}_${qn}`;
export const usePlayUrlCache = create<PlayUrlCacheStore>((_set, getState) => ({
cache: new Map(),
get: (bvid, qn) => {
const map = getState().cache;
const entry = map.get(makeKey(bvid, qn));
if (!entry) return undefined;
if (Date.now() - entry.ts > TTL_MS) {
map.delete(makeKey(bvid, qn));
return undefined;
}
return entry;
},
set: (bvid, qn, entry) => {
const map = getState().cache;
map.set(makeKey(bvid, qn), { ...entry, ts: Date.now() });
// 简单 LRU超过容量删最早
if (map.size > MAX_ENTRIES) {
const oldestKey = map.keys().next().value;
if (oldestKey) map.delete(oldestKey);
}
},
}));