From b1fda3a1a59189fd6665c370cddfe74daa9038c8 Mon Sep 17 00:00:00 2001 From: saravanakumardb1 Date: Thu, 19 Feb 2026 15:28:07 -0800 Subject: [PATCH] =?UTF-8?q?perf(local-llm):=20Sprint=205=20=E2=80=94=20req?= =?UTF-8?q?uest=20dedup=20+=20cache=20TTLs=20(P1,=20P2,=20P3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Performance fixes: - P1: Add fetchingRef guard to fetchAll() — prevents duplicate requests from rapid Refresh button clicks or overlapping interval ticks - P2: Add 5-minute TTL to staticCache (chip, GPU, brew packages) — previously cached indefinitely per server process, now refreshes after brew upgrades without requiring a restart - P3: Add 60-second TTL cache for Ollama models disk usage (du command) — previously traversed ~/.ollama/models on every 15s refresh cycle, now reuses cached value for 60s --- .../dashboard/src/app/api/system/route.ts | 21 +++++++++++++++---- __LOCAL_LLMs/dashboard/src/app/page.tsx | 4 ++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/__LOCAL_LLMs/dashboard/src/app/api/system/route.ts b/__LOCAL_LLMs/dashboard/src/app/api/system/route.ts index 5d44624b..7e6b71bb 100644 --- a/__LOCAL_LLMs/dashboard/src/app/api/system/route.ts +++ b/__LOCAL_LLMs/dashboard/src/app/api/system/route.ts @@ -6,12 +6,17 @@ import os from 'os'; const execAsync = promisify(exec); const execFileAsync = promisify(execFile); -// Cache slow commands (chip, gpu, brew don't change during a session) +// Cache slow commands with TTL let staticCache: { chip: string; gpu: string; brewPackages: Array<{ name: string; version: string }>; + ts: number; } | null = null; +const STATIC_TTL = 5 * 60 * 1000; // 5 minutes + +let ollamaDiskCache: { value: number; ts: number } | null = null; +const OLLAMA_DISK_TTL = 60 * 1000; // 60 seconds async function getChipInfo(): Promise { try { @@ -80,16 +85,24 @@ async function getBrewPackages(): Promise { + if (ollamaDiskCache && Date.now() - ollamaDiskCache.ts < OLLAMA_DISK_TTL) + return ollamaDiskCache.value; + const value = await getOllamaModelsDiskUsage(); + ollamaDiskCache = { value, ts: Date.now() }; + return value; +} + // macOS vm_stat gives accurate memory breakdown (os.freemem() excludes reclaimable cache) async function getAccurateMemory(): Promise<{ total: number; @@ -140,7 +153,7 @@ export async function GET() { const [staticInfo, disk, ollamaDisk, memory] = await Promise.all([ getStaticInfo(), getDiskSpace(), - getOllamaModelsDiskUsage(), + getCachedOllamaDiskUsage(), getAccurateMemory(), ]); diff --git a/__LOCAL_LLMs/dashboard/src/app/page.tsx b/__LOCAL_LLMs/dashboard/src/app/page.tsx index 825dcae8..5b5f321a 100644 --- a/__LOCAL_LLMs/dashboard/src/app/page.tsx +++ b/__LOCAL_LLMs/dashboard/src/app/page.tsx @@ -68,6 +68,7 @@ export default function Dashboard() { const [modelfileData, setModelfileData] = useState>({}); const responseRef = useRef(null); const abortRef = useRef(null); + const fetchingRef = useRef(false); const addToast = useCallback((message: string, type: Toast['type'] = 'info') => { const id = Date.now() + Math.random(); @@ -76,6 +77,8 @@ export default function Dashboard() { }, []); const fetchAll = useCallback(async () => { + if (fetchingRef.current) return; + fetchingRef.current = true; setLoading(true); const [oRes, wRes, sRes] = await Promise.allSettled([ fetch('/api/ollama').then(r => r.json()), @@ -87,6 +90,7 @@ export default function Dashboard() { if (sRes.status === 'fulfilled') setSystem(sRes.value); setLastRefresh(new Date()); setLoading(false); + fetchingRef.current = false; }, []); useEffect(() => {