mirror of
https://seed.radicle.dev/rad:z4D5UCArafTzTQpDZNQRuqswh3ury.git
synced 2026-11-03 18:40:29 +08:00
22 lines
615 B
TypeScript
22 lines
615 B
TypeScript
import { LRUCache } from "lru-cache";
|
|
|
|
export function cached<Args extends unknown[], V>(
|
|
f: (...args: Args) => Promise<V>,
|
|
makeKey: (...args: Args) => string,
|
|
options?: LRUCache.Options<string, { value: V }, unknown>,
|
|
): (...args: Args) => Promise<V> {
|
|
const cache = new LRUCache(options || { max: 500 });
|
|
return async function (...args: Args): Promise<V> {
|
|
const key = makeKey(...args);
|
|
const cached = cache.get(key);
|
|
|
|
if (cached === undefined) {
|
|
const value = await f(...args);
|
|
cache.set(key, { value });
|
|
return value;
|
|
} else {
|
|
return cached.value;
|
|
}
|
|
};
|
|
}
|