mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-10 09:04:23 +01:00
Improve the NIP-05 cache
This commit is contained in:
37
src/utils/SimpleLRU.ts
Normal file
37
src/utils/SimpleLRU.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
// deno-lint-ignore-file ban-types
|
||||
|
||||
import { LRUCache, type MapCache } from '@/deps.ts';
|
||||
|
||||
type FetchFn<K extends {}, V extends {}, O extends {}> = (key: K, opts: O) => Promise<V>;
|
||||
|
||||
interface FetchFnOpts {
|
||||
signal?: AbortSignal | null;
|
||||
}
|
||||
|
||||
export class SimpleLRU<
|
||||
K extends {},
|
||||
V extends {},
|
||||
O extends {} = FetchFnOpts,
|
||||
> implements MapCache<K, V, O> {
|
||||
protected cache: LRUCache<K, V, void>;
|
||||
|
||||
constructor(fetchFn: FetchFn<K, V, { signal: AbortSignal }>, opts: LRUCache.Options<K, V, void>) {
|
||||
this.cache = new LRUCache({
|
||||
fetchMethod: (key, _staleValue, { signal }) => fetchFn(key, { signal: signal as AbortSignal }),
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
|
||||
async fetch(key: K, opts?: O): Promise<V> {
|
||||
const result = await this.cache.fetch(key, opts);
|
||||
if (result === undefined) {
|
||||
throw new Error('SimpleLRU: fetch failed');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
put(key: K, value: V): Promise<void> {
|
||||
this.cache.set(key, value);
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user