max.js 737 B

123456789101112131415161718192021222324252627
  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 = (options.async && extensions.async) || (options.promise && extensions.promise)
  12. ? "async" : "";
  13. conf.on("set" + postfix, hit = function (id) {
  14. id = queue.hit(id);
  15. if (id === undefined) return;
  16. conf.delete(id);
  17. });
  18. conf.on("get" + postfix, hit);
  19. conf.on("delete" + postfix, queue.delete);
  20. conf.on("clear" + postfix, queue.clear);
  21. };