mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Merge branch 'fix-lru' into 'main'
SimpleLRU: fix repeated calls fetching See merge request soapbox-pub/ditto!510
This commit is contained in:
commit
cccb3ad8d7
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>) {
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue