Skip to content

Commit

Permalink
refactor: continue cache calculation in the background, even in timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsambugs committed Jun 23, 2024
1 parent 39901fa commit dffbe43
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/shared/concurrent-lru-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class ConcurrentLRUCacheWithContext<Context, Key extends ValidKey, Value>
// Try to calculate missing values
const toCalculate = notInCache.filter((key) => !this.beingCalculated.has(storableKeys[key]));
if (toCalculate.length > 0) {
const calculated = timeoutPromise(this.calculate(context, toCalculate), timeout);
const calculated = this.calculate(context, toCalculate);
for (const key of toCalculate) {
const storableKey = storableKeys[key];
const promise = calculated
Expand All @@ -165,7 +165,9 @@ export class ConcurrentLRUCacheWithContext<Context, Key extends ValidKey, Value>
// Wait for all calculations
const calculationPromises = notInCache.map<Promise<[Key, Value | undefined]>>(async (key) => [
key,
await this.beingCalculated.get(storableKeys[key]),
// Note: we add the timeout here so that even though this particular request might timeout, the result is still being made in the background
// Once it's done, we'll fill the cache for the next time
await timeoutPromise(this.beingCalculated.get(storableKeys[key]) ?? Promise.resolve(undefined), timeout).catch(() => undefined),
]);
const calculated = Object.fromEntries(await Promise.all(calculationPromises)) as Record<Key, Value | undefined>;

Expand Down

0 comments on commit dffbe43

Please sign in to comment.