视频源缓存更改为内存缓存

This commit is contained in:
mtvpls
2026-04-18 00:50:42 +08:00
parent 308650f771
commit 31af8a6814
+14 -19
View File
@@ -18,6 +18,7 @@ const REGISTRY_CACHE_TTL_MS = 4 * 60 * 60 * 1000;
const _compiledCache = new Map<string, any>(); const _compiledCache = new Map<string, any>();
const MAX_COMPILED_CACHE_SIZE = 50; const MAX_COMPILED_CACHE_SIZE = 50;
const _runtimeCache = new Map<string, { value: string; expiresAt: number }>();
export interface SourceScriptRecord { export interface SourceScriptRecord {
id: string; id: string;
@@ -178,6 +179,7 @@ async function loadRegistry(): Promise<SourceScriptRegistry> {
async function saveRegistry(registry: SourceScriptRegistry) { async function saveRegistry(registry: SourceScriptRegistry) {
_registryCache = null; _registryCache = null;
_compiledCache.clear(); _compiledCache.clear();
_runtimeCache.clear();
await db.setGlobalValue( await db.setGlobalValue(
SOURCE_SCRIPT_REGISTRY_KEY, SOURCE_SCRIPT_REGISTRY_KEY,
JSON.stringify(registry) JSON.stringify(registry)
@@ -235,33 +237,26 @@ function createCacheHelpers(scriptId: string) {
return { return {
async get(key: string) { async get(key: string) {
const raw = await db.getGlobalValue(`${prefix}${key}`); const entry = _runtimeCache.get(`${prefix}${key}`);
if (!raw) { if (!entry) {
return null; return null;
} }
try { if (entry.expiresAt && entry.expiresAt < Date.now()) {
const parsed = JSON.parse(raw) as { value: string; expiresAt: number }; _runtimeCache.delete(`${prefix}${key}`);
if (parsed.expiresAt && parsed.expiresAt < Date.now()) { return null;
await db.deleteGlobalValue(`${prefix}${key}`);
return null;
}
return parsed.value ?? null;
} catch {
return raw;
} }
return entry.value ?? null;
}, },
async set(key: string, value: string, ttlSec = 300) { async set(key: string, value: string, ttlSec = 300) {
await db.setGlobalValue( _runtimeCache.set(`${prefix}${key}`, {
`${prefix}${key}`, value,
JSON.stringify({ expiresAt: Date.now() + ttlSec * 1000,
value, });
expiresAt: Date.now() + ttlSec * 1000,
})
);
}, },
async del(key: string) { async del(key: string) {
await db.deleteGlobalValue(`${prefix}${key}`); _runtimeCache.delete(`${prefix}${key}`);
}, },
}; };
} }