diff options
author | isaacs <i@izs.me> | 2012-08-13 20:46:32 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-08-13 20:46:32 +0400 |
commit | 447303cc9f73f0ffdb1aa8c25b08077ba63a0747 (patch) | |
tree | 4af7d0b7fe012f88f3395e9bf7c705a1add695bd /node_modules/lru-cache | |
parent | 2195f8389aa181ea1cc68922d9fad8bf8a04d7a2 (diff) |
update lru-cache
Diffstat (limited to 'node_modules/lru-cache')
-rw-r--r-- | node_modules/lru-cache/README.md | 53 | ||||
-rw-r--r-- | node_modules/lru-cache/lib/lru-cache.js | 72 | ||||
-rw-r--r-- | node_modules/lru-cache/package.json | 8 | ||||
-rw-r--r-- | node_modules/lru-cache/test/basic.js | 78 |
4 files changed, 146 insertions, 65 deletions
diff --git a/node_modules/lru-cache/README.md b/node_modules/lru-cache/README.md index 7033fea89..8ea0dd963 100644 --- a/node_modules/lru-cache/README.md +++ b/node_modules/lru-cache/README.md @@ -2,32 +2,45 @@ A cache object that deletes the least-recently-used items. -Usage: +## Usage: - var LRU = require("lru-cache") - , cache = LRU(10, // max length. default = Infinity +```javascript +var LRU = require("lru-cache") + , options = { max: 500 + , length: function (n) { return n * 2 } + , dispose: function (key, n) { n.close() } + , maxAge: 1000 * 60 * 60 } + , cache = LRU(options) + , otherCache = LRU(50) // sets just the max size - // calculate how "big" each item is - // - // defaults to function(){return 1}, ie, just limit - // the item count, without any knowledge as to their - // relative size. - function (item) { return item.length }, +cache.set("key", "value") +cache.get("key") // "value" - // maxAge in ms - // defaults to infinite - // items are not pre-emptively pruned, but they - // are deleted when fetched if they're too old. - 1000 * 60) - - cache.set("key", "value") - cache.get("key") // "value" - - cache.reset() // empty the cache +cache.reset() // empty the cache +``` If you put more stuff in it, then items will fall out. If you try to put an oversized thing in it, then it'll fall out right away. -RTFS for more info. +## Options + +* `max` The maximum number of items. Not setting this is kind of + silly, since that's the whole purpose of this lib, but it defaults + to `Infinity`. +* `maxAge` Maximum age in ms. Items are not pro-actively pruned out + as they age, but if you try to get an item that is too old, it'll + drop it and return undefined instead of giving it to you. +* `length` Function that is used to calculate the length of stored + items. If you're storing strings or buffers, then you probably want + to do something like `function(n){return n.length}`. The default is + `function(n){return 1}`, which is fine if you want to store `n` + like-sized things. +* `dispose` Function that is called on items when they are dropped + from the cache. This can be handy if you want to close file + descriptors or do other cleanup tasks when items are no longer + accessible. Called with `key, value`. It's called *before* + actually removing the item from the internal cache, so if you want + to immediately put it back in, you'll have to do that in a + `nextTick` or `setTimeout` callback or it won't do anything. diff --git a/node_modules/lru-cache/lib/lru-cache.js b/node_modules/lru-cache/lib/lru-cache.js index 407ee6857..47b72baee 100644 --- a/node_modules/lru-cache/lib/lru-cache.js +++ b/node_modules/lru-cache/lib/lru-cache.js @@ -13,18 +13,32 @@ function hOP (obj, key) { function naiveLength () { return 1 } -function LRUCache (maxLength, lengthCalculator, maxAge) { +function LRUCache (options) { if (!(this instanceof LRUCache)) { - return new LRUCache(maxLength, lengthCalculator) + return new LRUCache(options) } + var max + if (typeof options === 'number') { + max = options + options = {max: max} + } + max = options.max + + if (!options) options = {} + + lengthCalculator = options.length || naiveLength + if (typeof lengthCalculator !== "function") { lengthCalculator = naiveLength } - if (!maxLength || !(typeof maxLength === "number") || maxLength <= 0 ) { - maxLength = Infinity + if (!max || !(typeof max === "number") || max <= 0 ) { + max = Infinity } + var maxAge = options.maxAge || null + + var dispose = options.dispose var cache = {} // hash of items by key , lruList = {} // list of items in order of use recency @@ -34,16 +48,16 @@ function LRUCache (maxLength, lengthCalculator, maxAge) { , itemCount = 0 - // resize the cache when the maxLength changes. - Object.defineProperty(this, "maxLength", + // resize the cache when the max changes. + Object.defineProperty(this, "max", { set : function (mL) { if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity - maxLength = mL - // if it gets above double maxLength, trim right away. + max = mL + // if it gets above double max, trim right away. // otherwise, do it whenever it's convenient. - if (length > maxLength) trim() + if (length > max) trim() } - , get : function () { return maxLength } + , get : function () { return max } , enumerable : true }) @@ -65,7 +79,7 @@ function LRUCache (maxLength, lengthCalculator, maxAge) { }) } - if (length > maxLength) trim() + if (length > max) trim() } , get : function () { return lengthCalculator } , enumerable : true @@ -83,6 +97,11 @@ function LRUCache (maxLength, lengthCalculator, maxAge) { }) this.reset = function () { + if (dispose) { + Object.keys(cache).forEach(function (k) { + dispose(k, cache[k].value) + }) + } cache = {} lruList = {} lru = 0 @@ -99,29 +118,37 @@ function LRUCache (maxLength, lengthCalculator, maxAge) { this.set = function (key, value) { if (hOP(cache, key)) { - this.get(key) + // dispose of the old one before overwriting + if (dispose) dispose(key, cache[key].value) + if (maxAge) cache[key].now = Date.now() cache[key].value = value + this.get(key) return true } - var hit = {key:key, value:value, lu:mru++, length:lengthCalculator(value)} - if (maxAge) hit.now = Date.now() + var hit = { + key:key, + value:value, + lu:mru++, + length:lengthCalculator(value), + now: (maxAge) ? Date.now() : 0 + } // oversized objects fall out of cache automatically. - if (hit.length > maxLength) return false + if (hit.length > max) return false length += hit.length lruList[hit.lu] = cache[key] = hit itemCount ++ - if (length > maxLength) trim() + if (length > max) trim() return true } this.get = function (key) { if (!hOP(cache, key)) return var hit = cache[key] - if (maxAge && Date.now() - hit.now > maxAge) { + if (maxAge && (Date.now() - hit.now > maxAge)) { this.del(key) return } @@ -135,6 +162,7 @@ function LRUCache (maxLength, lengthCalculator, maxAge) { this.del = function (key) { if (!hOP(cache, key)) return var hit = cache[key] + if (dispose) dispose(key, hit.value) delete cache[key] delete lruList[hit.lu] if (hit.lu === lru) lruWalk() @@ -148,11 +176,13 @@ function LRUCache (maxLength, lengthCalculator, maxAge) { } function trim () { - if (length <= maxLength) return + if (length <= max) return var prune = Object.keys(lruList) - for (var i = 0; i < prune.length && length > maxLength; i ++) { - length -= lruList[prune[i]].length - delete cache[ lruList[prune[i]].key ] + for (var i = 0; i < prune.length && length > max; i ++) { + var hit = lruList[prune[i]] + if (dispose) dispose(hit.key, hit.value) + length -= hit.length + delete cache[ hit.key ] delete lruList[prune[i]] } diff --git a/node_modules/lru-cache/package.json b/node_modules/lru-cache/package.json index 73c011b72..9af46c417 100644 --- a/node_modules/lru-cache/package.json +++ b/node_modules/lru-cache/package.json @@ -1,7 +1,7 @@ { "name": "lru-cache", "description": "A cache object that deletes the least-recently-used items.", - "version": "1.1.1", + "version": "2.0.0", "author": { "name": "Isaac Z. Schlueter", "email": "i@izs.me" @@ -47,7 +47,7 @@ "email": "marco.rogers@gmail.com" } ], - "readme": "# lru cache\n\nA cache object that deletes the least-recently-used items.\n\nUsage:\n\n var LRU = require(\"lru-cache\")\n , cache = LRU(10, // max length. default = Infinity\n\n // calculate how \"big\" each item is\n //\n // defaults to function(){return 1}, ie, just limit\n // the item count, without any knowledge as to their\n // relative size.\n function (item) { return item.length },\n\n // maxAge in ms\n // defaults to infinite\n // items are not pre-emptively pruned, but they\n // are deleted when fetched if they're too old.\n 1000 * 60)\n\n cache.set(\"key\", \"value\")\n cache.get(\"key\") // \"value\"\n\n cache.reset() // empty the cache\n\nIf you put more stuff in it, then items will fall out.\n\nIf you try to put an oversized thing in it, then it'll fall out right\naway.\n\nRTFS for more info.\n", - "_id": "lru-cache@1.1.1", - "_from": "lru-cache@~1.1.0" + "readme": "# lru cache\n\nA cache object that deletes the least-recently-used items.\n\n## Usage:\n\n```javascript\nvar LRU = require(\"lru-cache\")\n , options = { max: 500\n , length: function (n) { return n * 2 }\n , dispose: function (key, n) { n.close() }\n , maxAge: 1000 * 60 * 60 }\n , cache = LRU(options)\n , otherCache = LRU(50) // sets just the max size\n\ncache.set(\"key\", \"value\")\ncache.get(\"key\") // \"value\"\n\ncache.reset() // empty the cache\n```\n\nIf you put more stuff in it, then items will fall out.\n\nIf you try to put an oversized thing in it, then it'll fall out right\naway.\n\n## Options\n\n* `max` The maximum number of items. Not setting this is kind of\n silly, since that's the whole purpose of this lib, but it defaults\n to `Infinity`.\n* `maxAge` Maximum age in ms. Items are not pro-actively pruned out\n as they age, but if you try to get an item that is too old, it'll\n drop it and return undefined instead of giving it to you.\n* `length` Function that is used to calculate the length of stored\n items. If you're storing strings or buffers, then you probably want\n to do something like `function(n){return n.length}`. The default is\n `function(n){return 1}`, which is fine if you want to store `n`\n like-sized things.\n* `dispose` Function that is called on items when they are dropped\n from the cache. This can be handy if you want to close file\n descriptors or do other cleanup tasks when items are no longer\n accessible. Called with `key, value`. It's called *before*\n actually removing the item from the internal cache, so if you want\n to immediately put it back in, you'll have to do that in a\n `nextTick` or `setTimeout` callback or it won't do anything.\n", + "_id": "lru-cache@2.0.0", + "_from": "lru-cache@latest" } diff --git a/node_modules/lru-cache/test/basic.js b/node_modules/lru-cache/test/basic.js index c5b25de8b..d04af0bbc 100644 --- a/node_modules/lru-cache/test/basic.js +++ b/node_modules/lru-cache/test/basic.js @@ -2,12 +2,12 @@ var test = require("tap").test , LRU = require("../") test("basic", function (t) { - var cache = new LRU(10) + var cache = new LRU({max: 10}) cache.set("key", "value") t.equal(cache.get("key"), "value") t.equal(cache.get("nada"), undefined) t.equal(cache.length, 1) - t.equal(cache.maxLength, 10) + t.equal(cache.max, 10) t.end() }) @@ -42,17 +42,17 @@ test("del", function (t) { t.end() }) -test("maxLength", function (t) { +test("max", function (t) { var cache = new LRU(3) - // test changing the maxLength, verify that the LRU items get dropped. - cache.maxLength = 100 + // test changing the max, verify that the LRU items get dropped. + cache.max = 100 for (var i = 0; i < 100; i ++) cache.set(i, i) t.equal(cache.length, 100) for (var i = 0; i < 100; i ++) { t.equal(cache.get(i), i) } - cache.maxLength = 3 + cache.max = 3 t.equal(cache.length, 3) for (var i = 0; i < 97; i ++) { t.equal(cache.get(i), undefined) @@ -61,15 +61,15 @@ test("maxLength", function (t) { t.equal(cache.get(i), i) } - // now remove the maxLength restriction, and try again. - cache.maxLength = "hello" + // now remove the max restriction, and try again. + cache.max = "hello" for (var i = 0; i < 100; i ++) cache.set(i, i) t.equal(cache.length, 100) for (var i = 0; i < 100; i ++) { t.equal(cache.get(i), i) } // should trigger an immediate resize - cache.maxLength = 3 + cache.max = 3 t.equal(cache.length, 3) for (var i = 0; i < 97; i ++) { t.equal(cache.get(i), undefined) @@ -86,7 +86,7 @@ test("reset", function (t) { cache.set("b", "B") cache.reset() t.equal(cache.length, 0) - t.equal(cache.maxLength, 10) + t.equal(cache.max, 10) t.equal(cache.get("a"), undefined) t.equal(cache.get("b"), undefined) t.end() @@ -119,20 +119,26 @@ test("dump", function (t) { test("basic with weighed length", function (t) { - var cache = new LRU(100, function (item) { return item.size } ) + var cache = new LRU({ + max: 100, + length: function (item) { return item.size } + }) cache.set("key", {val: "value", size: 50}) t.equal(cache.get("key").val, "value") t.equal(cache.get("nada"), undefined) t.equal(cache.lengthCalculator(cache.get("key")), 50) t.equal(cache.length, 50) - t.equal(cache.maxLength, 100) + t.equal(cache.max, 100) t.end() }) test("weighed length item too large", function (t) { - var cache = new LRU(10, function (item) { return item.size } ) - t.equal(cache.maxLength, 10) + var cache = new LRU({ + max: 10, + length: function (item) { return item.size } + }) + t.equal(cache.max, 10) // should fall out immediately cache.set("key", {val: "value", size: 50}) @@ -143,7 +149,10 @@ test("weighed length item too large", function (t) { }) test("least recently set with weighed length", function (t) { - var cache = new LRU(8, function (item) { return item.length }) + var cache = new LRU({ + max:8, + length: function (item) { return item.length } + }) cache.set("a", "A") cache.set("b", "BB") cache.set("c", "CCC") @@ -156,7 +165,10 @@ test("least recently set with weighed length", function (t) { }) test("lru recently gotten with weighed length", function (t) { - var cache = new LRU(8, function (item) { return item.length }) + var cache = new LRU({ + max: 8, + length: function (item) { return item.length } + }) cache.set("a", "A") cache.set("b", "BB") cache.set("c", "CCC") @@ -171,10 +183,14 @@ test("lru recently gotten with weighed length", function (t) { }) test("set returns proper booleans", function(t) { - var cache = new LRU(5, function (item) { return item.length }) + var cache = new LRU({ + max: 5, + length: function (item) { return item.length } + }) + t.equal(cache.set("a", "A"), true) - // should return false for maxLength exceeded + // should return false for max exceeded t.equal(cache.set("b", "donuts"), false) t.equal(cache.set("b", "B"), true) @@ -183,7 +199,10 @@ test("set returns proper booleans", function(t) { }) test("drop the old items", function(t) { - var cache = new LRU(5, null, 50) + var cache = new LRU({ + max: 5, + maxAge: 50 + }) cache.set("a", "A") @@ -196,7 +215,7 @@ test("drop the old items", function(t) { cache.set("c", "C") // timed out t.notOk(cache.get("a")) - }, 51) + }, 60) setTimeout(function () { t.notOk(cache.get("b")) @@ -208,3 +227,22 @@ test("drop the old items", function(t) { t.end() }, 155) }) + +test("disposal function", function(t) { + var disposed = false + var cache = new LRU({ + max: 1, + dispose: function (k, n) { + disposed = n + } + }) + + cache.set(1, 1) + cache.set(2, 2) + t.equal(disposed, 1) + cache.set(3, 3) + t.equal(disposed, 2) + cache.reset() + t.equal(disposed, 3) + t.end() +}) |