mc-dashboard/lib/cache.ts

30 lines
853 B
TypeScript
Raw Permalink Normal View History

type Entry<T> = { value: T; expires: number };
const store = new Map<string, Entry<unknown>>();
export function memo<T>(key: string, ttlMs: number, fn: () => T): T {
const now = Date.now();
const hit = store.get(key) as Entry<T> | undefined;
if (hit && hit.expires > now) return hit.value;
const value = fn();
store.set(key, { value, expires: now + ttlMs });
return value;
}
export async function memoAsync<T>(
key: string,
ttlMs: number,
fn: () => Promise<T>
): Promise<T> {
const now = Date.now();
const hit = store.get(key) as Entry<T> | undefined;
if (hit && hit.expires > now) return hit.value;
const value = await fn();
store.set(key, { value, expires: now + ttlMs });
return value;
}
export function invalidate(prefix: string): void {
for (const k of store.keys()) if (k.startsWith(prefix)) store.delete(k);
}