All files / src/lib server-cache.ts

83.33% Statements 15/18
70% Branches 7/10
75% Functions 3/4
88.23% Lines 15/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                        3x             70x 70x   70x 2x       68x 1x 1x   1x 1x                   1x       67x 66x 66x         65x    
/**
 * Tiny in-memory stale-while-revalidate cache for slow server routes.
 *
 * - First call for a key blocks until the loader resolves.
 * - Subsequent calls within TTL return the cached value immediately.
 * - After TTL expires, callers receive the stale value and a background
 *   refresh fires; the next caller after refresh sees the new value.
 *
 * In-memory only — fine for single-replica ACA Container Apps. If we ever
 * scale the UI horizontally we should swap this for a shared cache (Redis
 * or NATS-backed). The caller-supplied key should encode anything that
 * varies per request (auth role, query params).
 */
 
interface CacheEntry<T> {
  value: T;
  freshUntil: number; // epoch ms
  refreshing?: Promise<T>;
}
 
const cache = new Map<string, CacheEntry<unknown>>();
 
export async function cached<T>(
  key: string,
  ttlMs: number,
  loader: () => Promise<T>,
): Promise<T> {
  const now = Date.now();
  const entry = cache.get(key) as CacheEntry<T> | undefined;
 
  if (entry && entry.freshUntil > now) {
    return entry.value;
  }
 
  // Stale-but-present: serve stale, kick off background refresh once.
  if (entry) {
    Eif (!entry.refreshing) {
      entry.refreshing = loader()
        .then((v) => {
          cache.set(key, { value: v, freshUntil: Date.now() + ttlMs });
          return v;
        })
        .catch((err) => {
          // Refresh failed — keep the stale entry so callers don't get a
          // stampede of failures. Reset the refreshing flag so a later
          // caller can retry.
          if (entry) entry.refreshing = undefined;
          throw err;
        });
    }
    return entry.value;
  }
 
  // Cold cache: must wait for the loader.
  const value = await loader();
  cache.set(key, { value, freshUntil: Date.now() + ttlMs });
  return value;
}
 
/** Test-only helper to clear the cache between specs. */
export function __resetCacheForTests() {
  cache.clear();
}