mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
SimpleLRU: fix repeated calls fetching
This commit is contained in:
parent
ce562b3b6a
commit
6745e96c64
2 changed files with 29 additions and 2 deletions
21
src/utils/SimpleLRU.test.ts
Normal file
21
src/utils/SimpleLRU.test.ts
Normal 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);
|
||||
});
|
||||
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue