Add caching for api calls to speed them up. Added caching for cpu and gpu stat calls

This commit is contained in:
Jaret Burkett committed 2026-07-16 16:47:10 -06:00
1 parent 3c75735ba2
commit bf15b65972
3 files changed
+83 -13

No files matched your search

+8 -2
View File
@@ -3,11 +3,11 @@ import si from 'systeminformation';
import { createRequire } from 'module'; import { createRequire } from 'module';
import os from 'os'; import os from 'os';
import { CpuInfo } from '@/types'; import { CpuInfo } from '@/types';
import { cached } from '@/server/apiCache';
const isMac = os.platform() === 'darwin'; const isMac = os.platform() === 'darwin';
export async function GET() { async function getCpuInfo(): Promise<CpuInfo> {
try {
const cpuInfoRaw = await si.cpu(); const cpuInfoRaw = await si.cpu();
let cpuInfo: CpuInfo; let cpuInfo: CpuInfo;
@@ -53,6 +53,12 @@ export async function GET() {
}; };
} }
return cpuInfo;
}
export async function GET() {
try {
const cpuInfo = await cached('cpu-info', getCpuInfo);
return NextResponse.json(cpuInfo); return NextResponse.json(cpuInfo);
} catch (error) { } catch (error) {
console.error('Error fetching CPU stats:', error); console.error('Error fetching CPU stats:', error);
+16 -11
View File
@@ -3,6 +3,7 @@ import { exec } from 'child_process';
import { promisify } from 'util'; import { promisify } from 'util';
import { createRequire } from 'module'; import { createRequire } from 'module';
import os from 'os'; import os from 'os';
import { cached } from '@/server/apiCache';
const execAsync = promisify(exec); const execAsync = promisify(exec);
@@ -93,8 +94,7 @@ async function getMacGpuInfo(): Promise<MacGpuResult | null> {
} }
} }
export async function GET() { async function getGpuInfo() {
try {
// Get platform // Get platform
const platform = os.platform(); const platform = os.platform();
const isWindows = platform === 'win32'; const isWindows = platform === 'win32';
@@ -103,7 +103,7 @@ export async function GET() {
if (isMac) { if (isMac) {
const macGpu = await getMacGpuInfo(); const macGpu = await getMacGpuInfo();
if (macGpu) { if (macGpu) {
return NextResponse.json({ return {
hasNvidiaSmi: false, hasNvidiaSmi: false,
isMac: true, isMac: true,
gpus: [ gpus: [
@@ -126,35 +126,41 @@ export async function GET() {
fan: { speed: macGpu.fanSpeed }, fan: { speed: macGpu.fanSpeed },
}, },
], ],
}); };
} }
return NextResponse.json({ return {
hasNvidiaSmi: false, hasNvidiaSmi: false,
isMac: true, isMac: true,
gpus: [], gpus: [],
error: 'Could not read Mac GPU stats', error: 'Could not read Mac GPU stats',
}); };
} }
// Check if nvidia-smi is available // Check if nvidia-smi is available
const hasNvidiaSmi = await checkNvidiaSmi(isWindows); const hasNvidiaSmi = await checkNvidiaSmi(isWindows);
if (!hasNvidiaSmi) { if (!hasNvidiaSmi) {
return NextResponse.json({ return {
hasNvidiaSmi: false, hasNvidiaSmi: false,
isMac: false, isMac: false,
gpus: [], gpus: [],
error: 'nvidia-smi not found or not accessible', error: 'nvidia-smi not found or not accessible',
}); };
} }
// Get GPU stats // Get GPU stats
const gpuStats = await getGpuStats(isWindows); const gpuStats = await getGpuStats(isWindows);
return NextResponse.json({ return {
hasNvidiaSmi: true, hasNvidiaSmi: true,
gpus: gpuStats, gpus: gpuStats,
}); };
}
export async function GET() {
try {
const gpuInfo = await cached('gpu-info', getGpuInfo);
return NextResponse.json(gpuInfo);
} catch (error) { } catch (error) {
console.error('Error fetching NVIDIA GPU stats:', error); console.error('Error fetching NVIDIA GPU stats:', error);
return NextResponse.json( return NextResponse.json(
@@ -248,4 +254,3 @@ async function getGpuStats(isWindows: boolean) {
return gpus; return gpus;
} }
+59
View File
@@ -0,0 +1,59 @@
type CacheEntry = {
promise: Promise<unknown>;
timestamp: number;
};
const cache = new Map<string, CacheEntry>();
const DEFAULT_STALE_TIME_MS = 5000;
/**
* Universal cache for slow API work. Results are cached per key + params and
* reused until they go stale. Concurrent callers while a fetch is in flight
* share the same promise instead of triggering duplicate work.
*
* @param key Unique name for this cached operation (e.g. 'cpu-info')
* @param fetcher Function that produces a fresh value
* @param staleTimeMs How long a cached value stays fresh (default 5000ms)
* @param params Optional params; different params get separate cache entries
*/
export async function cached<T>(
key: string,
fetcher: () => Promise<T>,
staleTimeMs: number = DEFAULT_STALE_TIME_MS,
params: unknown = null,
): Promise<T> {
const cacheKey = params === null ? key : `${key}:${JSON.stringify(params)}`;
const now = Date.now();
const entry = cache.get(cacheKey);
if (entry && now - entry.timestamp < staleTimeMs) {
return entry.promise as Promise<T>;
}
const promise = fetcher();
cache.set(cacheKey, { promise, timestamp: now });
// Drop failed fetches so the next call retries instead of caching the error
promise.catch(() => {
if (cache.get(cacheKey)?.promise === promise) {
cache.delete(cacheKey);
}
});
return promise;
}
/** Remove a cached entry (all param variants if no params given). */
export function invalidateCache(key: string, params: unknown = null): void {
if (params !== null) {
cache.delete(`${key}:${JSON.stringify(params)}`);
return;
}
cache.delete(key);
for (const cacheKey of cache.keys()) {
if (cacheKey.startsWith(`${key}:`)) {
cache.delete(cacheKey);
}
}
}