UNPKG

789 BJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.lru = void 0;
7const flru_1 = __importDefault(require("flru"));
8const cache = (0, flru_1.default)(3);
9/**
10 * A decorator to return new function with LRU cache.
11 */
12function lru(fn, keyFn = (...args) => `${args[0]}`, maxSize = 16) {
13 const cache = (0, flru_1.default)(maxSize);
14 return (...args) => {
15 const key = keyFn(...args);
16 let v = cache.get(key);
17 if (cache.has(key))
18 return cache.get(key);
19 v = fn(...args);
20 cache.set(key, v);
21 return v;
22 };
23}
24exports.lru = lru;
25//# sourceMappingURL=lru.js.map
\No newline at end of file