import LruCache from "lru-cache"; // Creates a function that memoizes its result using an LRU cache. // // The cache key is a string created from the arguments using // `makeKey`. export function cached( f: (...args: Args) => Promise, makeKey: (...args: Args) => string, options?: LruCache.Options, ): (...args: Args) => Promise { const cache = new LruCache(options || { max: 500 }); return async function (...args: Args): Promise { 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; } }; }