Files
EpicNext-Cms/src/lib/cache.ts
T
2026-07-20 14:47:05 +02:00

23 lines
588 B
TypeScript

type CacheEntry<T> = { data: T; expiresAt: number };
const store = new Map<string, CacheEntry<unknown>>();
export function cached<T>(
key: string,
ttlMs: number,
fn: () => Promise<T>,
): Promise<T> {
const existing = store.get(key);
if (existing && existing.expiresAt > Date.now()) {
return Promise.resolve(existing.data as T);
}
return fn().then((data) => {
store.set(key, { data, expiresAt: Date.now() + ttlMs });
return data;
});
}
// TODO: Check of dit weg kan — niet geïmporteerd in de codebase
export function bustCache(key: string): void {
store.delete(key);
}