MCPcopy Create free account
hub / github.com/monkeytypegame/monkeytype / cacheWithTTL

Function cacheWithTTL

backend/src/utils/ttl-cache.ts:13–27  ·  view source on GitHub ↗
(
  ttlMs: number,
  fn: () => Promise<T>,
)

Source from the content-addressed store, hash-verified

11 * @returns {() => Promise<T | undefined>}
12 */
13export function cacheWithTTL<T>(
14 ttlMs: number,
15 fn: () => Promise<T>,
16): () => Promise<T | undefined> {
17 let lastFetchTime = 0;
18 let cache: T | undefined;
19
20 return async () => {
21 if (lastFetchTime < Date.now() - ttlMs) {
22 lastFetchTime = Date.now();
23 cache = await fn();
24 }
25 return cache;
26 };
27}

Callers 1

psa.tsFile · 0.90

Calls 1

fnFunction · 0.85

Tested by

no test coverage detected