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(() => {