Merge branch 'fix-lru' into 'main'

SimpleLRU: fix repeated calls fetching

See merge request soapbox-pub/ditto!510
This commit is contained in:
Alex Gleason 2024-09-22 21:13:04 +00:00
commit cccb3ad8d7
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>) { constructor(fetchFn: FetchFn<K, V, { signal: AbortSignal }>, private opts: SimpleLRUOpts<K, V>) {
this.cache = new LRUCache({ 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, ...opts,
}); });
} }
@ -32,7 +38,7 @@ export class SimpleLRU<
this.opts.gauge?.set(this.cache.size); this.opts.gauge?.set(this.cache.size);
if (result === undefined) { if (result === undefined || result === null) {
throw new Error('SimpleLRU: fetch failed'); throw new Error('SimpleLRU: fetch failed');
} }