max.js 756 B

1234567891011121314151617181920212223242526272829303132
  1. // Limit cache size, LRU (least recently used) algorithm.
  2. "use strict";
  3. var toPosInteger = require("es5-ext/number/to-pos-integer")
  4. , lruQueue = require("lru-queue")
  5. , extensions = require("../lib/registered-extensions");
  6. extensions.max = function (max, conf, options) {
  7. var postfix, queue, hit;
  8. max = toPosInteger(max);
  9. if (!max) return;
  10. queue = lruQueue(max);
  11. postfix =
  12. (options.async && extensions.async) || (options.promise && extensions.promise)
  13. ? "async"
  14. : "";
  15. conf.on(
  16. "set" + postfix,
  17. (hit = function (id) {
  18. id = queue.hit(id);
  19. if (id === undefined) return;
  20. conf.delete(id);
  21. })
  22. );
  23. conf.on("get" + postfix, hit);
  24. conf.on("delete" + postfix, queue.delete);
  25. conf.on("clear" + postfix, queue.clear);
  26. };