SimpleLRU: fix repeated calls fetching

This commit is contained in:
Alex Gleason 2024-09-22 16:06:10 -05:00
parent ce562b3b6a
commit 6745e96c64
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 29 additions and 2 deletions

View file

@ -0,0 +1,21 @@
import { SimpleLRU } from '@/utils/SimpleLRU.ts';
import { assertEquals, assertRejects } from '@std/assert';
Deno.test("SimpleLRU doesn't repeat failed calls", async () => {
let calls = 0;
const cache = new SimpleLRU(
// deno-lint-ignore require-await
async () => {
calls++;
throw new Error('gg');
},
{ max: 100 },
);
await assertRejects(() => cache.fetch('foo'));
assertEquals(calls, 1);
await assertRejects(() => cache.fetch('foo'));
assertEquals(calls, 1);
});

View file

@ -22,7 +22,13 @@ export class SimpleLRU<
constructor(fetchFn: FetchFn<K, V, { signal: AbortSignal }>, private opts: SimpleLRUOpts<K, V>) {
this.cache = new LRUCache({
fetchMethod: (key, _staleValue, { signal }) => fetchFn(key, { signal: signal as unknown as AbortSignal }),
async fetchMethod(key, _staleValue, { signal }) {
try {
return await fetchFn(key, { signal: signal as unknown as AbortSignal });
} catch {
return null as unknown as V;
}
},
...opts,
});
}
@ -32,7 +38,7 @@ export class SimpleLRU<
this.opts.gauge?.set(this.cache.size);
if (result === undefined) {
if (result === undefined || result === null) {
throw new Error('SimpleLRU: fetch failed');
}