From 31af8a6814ea998c19c45d8f5865573b0a01f17e Mon Sep 17 00:00:00 2001 From: mtvpls Date: Sat, 18 Apr 2026 00:50:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=BA=90=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=B8=BA=E5=86=85=E5=AD=98=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/source-script.ts | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/lib/source-script.ts b/src/lib/source-script.ts index ab72966..40cc08a 100644 --- a/src/lib/source-script.ts +++ b/src/lib/source-script.ts @@ -18,6 +18,7 @@ const REGISTRY_CACHE_TTL_MS = 4 * 60 * 60 * 1000; const _compiledCache = new Map(); const MAX_COMPILED_CACHE_SIZE = 50; +const _runtimeCache = new Map(); export interface SourceScriptRecord { id: string; @@ -178,6 +179,7 @@ async function loadRegistry(): Promise { async function saveRegistry(registry: SourceScriptRegistry) { _registryCache = null; _compiledCache.clear(); + _runtimeCache.clear(); await db.setGlobalValue( SOURCE_SCRIPT_REGISTRY_KEY, JSON.stringify(registry) @@ -235,33 +237,26 @@ function createCacheHelpers(scriptId: string) { return { async get(key: string) { - const raw = await db.getGlobalValue(`${prefix}${key}`); - if (!raw) { + const entry = _runtimeCache.get(`${prefix}${key}`); + if (!entry) { return null; } - try { - const parsed = JSON.parse(raw) as { value: string; expiresAt: number }; - if (parsed.expiresAt && parsed.expiresAt < Date.now()) { - await db.deleteGlobalValue(`${prefix}${key}`); - return null; - } - return parsed.value ?? null; - } catch { - return raw; + if (entry.expiresAt && entry.expiresAt < Date.now()) { + _runtimeCache.delete(`${prefix}${key}`); + return null; } + + return entry.value ?? null; }, async set(key: string, value: string, ttlSec = 300) { - await db.setGlobalValue( - `${prefix}${key}`, - JSON.stringify({ - value, - expiresAt: Date.now() + ttlSec * 1000, - }) - ); + _runtimeCache.set(`${prefix}${key}`, { + value, + expiresAt: Date.now() + ttlSec * 1000, + }); }, async del(key: string) { - await db.deleteGlobalValue(`${prefix}${key}`); + _runtimeCache.delete(`${prefix}${key}`); }, }; }