lru.js 485 B

123456789101112131415161718
  1. import flru from 'flru';
  2. const cache = flru(3);
  3. /**
  4. * A decorator to return new function with LRU cache.
  5. */
  6. export function lru(fn, keyFn = (...args) => `${args[0]}`, maxSize = 16) {
  7. const cache = flru(maxSize);
  8. return (...args) => {
  9. const key = keyFn(...args);
  10. let v = cache.get(key);
  11. if (cache.has(key))
  12. return cache.get(key);
  13. v = fn(...args);
  14. cache.set(key, v);
  15. return v;
  16. };
  17. }
  18. //# sourceMappingURL=lru.js.map