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