pax_global_header00006660000000000000000000000064122045066500014512gustar00rootroot0000000000000052 comment=a81fabfeb5a53c2681bdafb20f57da05bd3cb48b node-lru-cache-2.3.1/000077500000000000000000000000001220450665000143035ustar00rootroot00000000000000node-lru-cache-2.3.1/.gitignore000066400000000000000000000000161220450665000162700ustar00rootroot00000000000000/node_modules node-lru-cache-2.3.1/AUTHORS000066400000000000000000000004661220450665000153610ustar00rootroot00000000000000# Authors, sorted by whether or not they are me Isaac Z. Schlueter Carlos Brito Lage Marko Mikulicic Trent Mick Kevin O'Hara Marco Rogers Jesse Dailey node-lru-cache-2.3.1/LICENSE000066400000000000000000000021041220450665000153050ustar00rootroot00000000000000Copyright 2009, 2010, 2011 Isaac Z. Schlueter. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-lru-cache-2.3.1/README.md000066400000000000000000000062111220450665000155620ustar00rootroot00000000000000# lru cache A cache object that deletes the least-recently-used items. ## Usage: ```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 cache.set("key", "value") cache.get("key") // "value" 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. ## Options * `max` The maximum size of the cache, checked by applying the length function to all values in the cache. 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. * `stale` By default, if you set a `maxAge`, it'll only actually pull stale items out of the cache when you `get(key)`. (That is, it's not pre-emptively doing a `setTimeout` or anything.) If you set `stale:true`, it'll return the stale value before deleting it. If you don't set this, then it'll return `undefined` when you try to get a stale entry, as if it had already been deleted. ## API * `set(key, value)` * `get(key) => value` Both of these will update the "recently used"-ness of the key. They do what you think. * `peek(key)` Returns the key value (or `undefined` if not found) without updating the "recently used"-ness of the key. (If you find yourself using this a lot, you *might* be using the wrong sort of data structure, but there are some use cases where it's handy.) * `del(key)` Deletes a key out of the cache. * `reset()` Clear the cache entirely, throwing away all values. * `has(key)` Check if a key is in the cache, without updating the recent-ness or deleting it for being stale. * `forEach(function(value,key,cache), [thisp])` Just like `Array.prototype.forEach`. Iterates over all the keys in the cache, in order of recent-ness. (Ie, more recently used items are iterated over first.) * `keys()` Return an array of the keys in the cache. * `values()` Return an array of the values in the cache. node-lru-cache-2.3.1/lib/000077500000000000000000000000001220450665000150515ustar00rootroot00000000000000node-lru-cache-2.3.1/lib/lru-cache.js000066400000000000000000000134571220450665000172640ustar00rootroot00000000000000;(function () { // closure for web browsers if (typeof module === 'object' && module.exports) { module.exports = LRUCache } else { // just set the global for non-node platforms. this.LRUCache = LRUCache } function hOP (obj, key) { return Object.prototype.hasOwnProperty.call(obj, key) } function naiveLength () { return 1 } function LRUCache (options) { if (!(this instanceof LRUCache)) { return new LRUCache(options) } var max if (typeof options === 'number') { max = options options = { max: max } } if (!options) options = {} max = options.max var lengthCalculator = options.length || naiveLength if (typeof lengthCalculator !== "function") { lengthCalculator = naiveLength } if (!max || !(typeof max === "number") || max <= 0 ) { // a little bit silly. maybe this should throw? max = Infinity } var allowStale = options.stale || false var maxAge = options.maxAge || null var dispose = options.dispose var cache = Object.create(null) // hash of items by key , lruList = Object.create(null) // list of items in order of use recency , mru = 0 // most recently used , lru = 0 // least recently used , length = 0 // number of items in the list , itemCount = 0 // resize the cache when the max changes. Object.defineProperty(this, "max", { set : function (mL) { if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity max = mL // if it gets above double max, trim right away. // otherwise, do it whenever it's convenient. if (length > max) trim() } , get : function () { return max } , enumerable : true }) // resize the cache when the lengthCalculator changes. Object.defineProperty(this, "lengthCalculator", { set : function (lC) { if (typeof lC !== "function") { lengthCalculator = naiveLength length = itemCount for (var key in cache) { cache[key].length = 1 } } else { lengthCalculator = lC length = 0 for (var key in cache) { cache[key].length = lengthCalculator(cache[key].value) length += cache[key].length } } if (length > max) trim() } , get : function () { return lengthCalculator } , enumerable : true }) Object.defineProperty(this, "length", { get : function () { return length } , enumerable : true }) Object.defineProperty(this, "itemCount", { get : function () { return itemCount } , enumerable : true }) this.forEach = function (fn, thisp) { thisp = thisp || this var i = 0; for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) { i++ var hit = lruList[k] if (maxAge && (Date.now() - hit.now > maxAge)) { del(hit) if (!allowStale) hit = undefined } if (hit) { fn.call(thisp, hit.value, hit.key, this) } } } this.keys = function () { var keys = new Array(itemCount) var i = 0 for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) { var hit = lruList[k] keys[i++] = hit.key } return keys } this.values = function () { var values = new Array(itemCount) var i = 0 for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) { var hit = lruList[k] values[i++] = hit.value } return values } this.reset = function () { if (dispose) { for (var k in cache) { dispose(k, cache[k].value) } } cache = {} lruList = {} lru = 0 mru = 0 length = 0 itemCount = 0 } // Provided for debugging/dev purposes only. No promises whatsoever that // this API stays stable. this.dump = function () { return cache } this.dumpLru = function () { return lruList } this.set = function (key, value) { if (hOP(cache, 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 len = lengthCalculator(value) var age = maxAge ? Date.now() : 0 var hit = new Entry(key, value, mru++, len, age) // oversized objects fall out of cache automatically. if (hit.length > max) { if (dispose) dispose(key, value) return false } length += hit.length lruList[hit.lu] = cache[key] = hit itemCount ++ if (length > max) trim() return true } this.has = function (key) { if (!hOP(cache, key)) return false var hit = cache[key] if (maxAge && (Date.now() - hit.now > maxAge)) { return false } return true } this.get = function (key) { return get(key, true) } this.peek = function (key) { return get(key, false) } function get (key, doUse) { var hit = cache[key] if (hit) { if (maxAge && (Date.now() - hit.now > maxAge)) { del(hit) if (!allowStale) hit = undefined } else { if (doUse) use(hit) } if (hit) hit = hit.value } return hit } function use (hit) { shiftLU(hit) hit.lu = mru ++ lruList[hit.lu] = hit } this.del = function (key) { del(cache[key]) } function trim () { while (lru < mru && length > max) del(lruList[lru]) } function shiftLU(hit) { delete lruList[ hit.lu ] while (lru < mru && !lruList[lru]) lru ++ } function del(hit) { if (hit) { if (dispose) dispose(hit.key, hit.value) length -= hit.length itemCount -- delete cache[ hit.key ] shiftLU(hit) } } } // classy, since V8 prefers predictable objects. function Entry (key, value, mru, len, age) { this.key = key this.value = value this.lu = mru this.length = len this.now = age } })() node-lru-cache-2.3.1/package.json000066400000000000000000000007411220450665000165730ustar00rootroot00000000000000{ "name": "lru-cache", "description": "A cache object that deletes the least-recently-used items.", "version": "2.3.1", "author": "Isaac Z. Schlueter ", "scripts": { "test": "tap test --gc" }, "main": "lib/lru-cache.js", "repository": "git://github.com/isaacs/node-lru-cache.git", "devDependencies": { "tap": "", "weak": "" }, "license": { "type": "MIT", "url": "http://github.com/isaacs/node-lru-cache/raw/master/LICENSE" } } node-lru-cache-2.3.1/test/000077500000000000000000000000001220450665000152625ustar00rootroot00000000000000node-lru-cache-2.3.1/test/basic.js000066400000000000000000000157011220450665000167050ustar00rootroot00000000000000var test = require("tap").test , LRU = require("../") test("basic", function (t) { 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.max, 10) t.end() }) test("least recently set", function (t) { var cache = new LRU(2) cache.set("a", "A") cache.set("b", "B") cache.set("c", "C") t.equal(cache.get("c"), "C") t.equal(cache.get("b"), "B") t.equal(cache.get("a"), undefined) t.end() }) test("lru recently gotten", function (t) { var cache = new LRU(2) cache.set("a", "A") cache.set("b", "B") cache.get("a") cache.set("c", "C") t.equal(cache.get("c"), "C") t.equal(cache.get("b"), undefined) t.equal(cache.get("a"), "A") t.end() }) test("del", function (t) { var cache = new LRU(2) cache.set("a", "A") cache.del("a") t.equal(cache.get("a"), undefined) t.end() }) test("max", function (t) { var cache = new LRU(3) // 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.max = 3 t.equal(cache.length, 3) for (var i = 0; i < 97; i ++) { t.equal(cache.get(i), undefined) } for (var i = 98; i < 100; i ++) { t.equal(cache.get(i), i) } // 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.max = 3 t.equal(cache.length, 3) for (var i = 0; i < 97; i ++) { t.equal(cache.get(i), undefined) } for (var i = 98; i < 100; i ++) { t.equal(cache.get(i), i) } t.end() }) test("reset", function (t) { var cache = new LRU(10) cache.set("a", "A") cache.set("b", "B") cache.reset() t.equal(cache.length, 0) t.equal(cache.max, 10) t.equal(cache.get("a"), undefined) t.equal(cache.get("b"), undefined) t.end() }) // Note: `.dump()` is a debugging tool only. No guarantees are made // about the format/layout of the response. test("dump", function (t) { var cache = new LRU(10) var d = cache.dump(); t.equal(Object.keys(d).length, 0, "nothing in dump for empty cache") cache.set("a", "A") var d = cache.dump() // { a: { key: "a", value: "A", lu: 0 } } t.ok(d.a) t.equal(d.a.key, "a") t.equal(d.a.value, "A") t.equal(d.a.lu, 0) cache.set("b", "B") cache.get("b") d = cache.dump() t.ok(d.b) t.equal(d.b.key, "b") t.equal(d.b.value, "B") t.equal(d.b.lu, 2) t.end() }) test("basic with weighed length", function (t) { 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.max, 100) t.end() }) test("weighed length item too large", function (t) { 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}) t.equal(cache.length, 0) t.equal(cache.get("key"), undefined) t.end() }) test("least recently set with weighed length", function (t) { var cache = new LRU({ max:8, length: function (item) { return item.length } }) cache.set("a", "A") cache.set("b", "BB") cache.set("c", "CCC") cache.set("d", "DDDD") t.equal(cache.get("d"), "DDDD") t.equal(cache.get("c"), "CCC") t.equal(cache.get("b"), undefined) t.equal(cache.get("a"), undefined) t.end() }) test("lru recently gotten with weighed length", function (t) { var cache = new LRU({ max: 8, length: function (item) { return item.length } }) cache.set("a", "A") cache.set("b", "BB") cache.set("c", "CCC") cache.get("a") cache.get("b") cache.set("d", "DDDD") t.equal(cache.get("c"), undefined) t.equal(cache.get("d"), "DDDD") t.equal(cache.get("b"), "BB") t.equal(cache.get("a"), "A") t.end() }) test("set returns proper booleans", function(t) { var cache = new LRU({ max: 5, length: function (item) { return item.length } }) t.equal(cache.set("a", "A"), true) // should return false for max exceeded t.equal(cache.set("b", "donuts"), false) t.equal(cache.set("b", "B"), true) t.equal(cache.set("c", "CCCC"), true) t.end() }) test("drop the old items", function(t) { var cache = new LRU({ max: 5, maxAge: 50 }) cache.set("a", "A") setTimeout(function () { cache.set("b", "b") t.equal(cache.get("a"), "A") }, 25) setTimeout(function () { cache.set("c", "C") // timed out t.notOk(cache.get("a")) }, 60) setTimeout(function () { t.notOk(cache.get("b")) t.equal(cache.get("c"), "C") }, 90) setTimeout(function () { t.notOk(cache.get("c")) 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() }) test("disposal function on too big of item", function(t) { var disposed = false var cache = new LRU({ max: 1, length: function (k) { return k.length }, dispose: function (k, n) { disposed = n } }) var obj = [ 1, 2 ] t.equal(disposed, false) cache.set("obj", obj) t.equal(disposed, obj) t.end() }) test("has()", function(t) { var cache = new LRU({ max: 1, maxAge: 10 }) cache.set('foo', 'bar') t.equal(cache.has('foo'), true) cache.set('blu', 'baz') t.equal(cache.has('foo'), false) t.equal(cache.has('blu'), true) setTimeout(function() { t.equal(cache.has('blu'), false) t.end() }, 15) }) test("stale", function(t) { var cache = new LRU({ maxAge: 10, stale: true }) cache.set('foo', 'bar') t.equal(cache.get('foo'), 'bar') t.equal(cache.has('foo'), true) setTimeout(function() { t.equal(cache.has('foo'), false) t.equal(cache.get('foo'), 'bar') t.equal(cache.get('foo'), undefined) t.end() }, 15) }) test("lru update via set", function(t) { var cache = LRU({ max: 2 }); cache.set('foo', 1); cache.set('bar', 2); cache.del('bar'); cache.set('baz', 3); cache.set('qux', 4); t.equal(cache.get('foo'), undefined) t.equal(cache.get('bar'), undefined) t.equal(cache.get('baz'), 3) t.equal(cache.get('qux'), 4) t.end() }) test("least recently set w/ peek", function (t) { var cache = new LRU(2) cache.set("a", "A") cache.set("b", "B") t.equal(cache.peek("a"), "A") cache.set("c", "C") t.equal(cache.get("c"), "C") t.equal(cache.get("b"), "B") t.equal(cache.get("a"), undefined) t.end() }) node-lru-cache-2.3.1/test/foreach.js000066400000000000000000000020501220450665000172240ustar00rootroot00000000000000var test = require('tap').test var LRU = require('../') test('forEach', function (t) { var l = new LRU(5) for (var i = 0; i < 10; i ++) { l.set(i.toString(), i.toString(2)) } var i = 9 l.forEach(function (val, key, cache) { t.equal(cache, l) t.equal(key, i.toString()) t.equal(val, i.toString(2)) i -= 1 }) // get in order of most recently used l.get(6) l.get(8) var order = [ 8, 6, 9, 7, 5 ] var i = 0 l.forEach(function (val, key, cache) { var j = order[i ++] t.equal(cache, l) t.equal(key, j.toString()) t.equal(val, j.toString(2)) }) t.end() }) test('keys() and values()', function (t) { var l = new LRU(5) for (var i = 0; i < 10; i ++) { l.set(i.toString(), i.toString(2)) } t.similar(l.keys(), ['9', '8', '7', '6', '5']) t.similar(l.values(), ['1001', '1000', '111', '110', '101']) // get in order of most recently used l.get(6) l.get(8) t.similar(l.keys(), ['8', '6', '9', '7', '5']) t.similar(l.values(), ['1000', '110', '1001', '111', '101']) t.end() }) node-lru-cache-2.3.1/test/memory-leak.js000066400000000000000000000015571220450665000200520ustar00rootroot00000000000000#!/usr/bin/env node --expose_gc var weak = require('weak'); var test = require('tap').test var LRU = require('../') var l = new LRU({ max: 10 }) var refs = 0 function X() { refs ++ weak(this, deref) } function deref() { refs -- } test('no leaks', function (t) { // fill up the cache for (var i = 0; i < 100; i++) { l.set(i, new X); // throw some gets in there, too. if (i % 2 === 0) l.get(i / 2) } gc() var start = process.memoryUsage() // capture the memory var startRefs = refs // do it again, but more for (var i = 0; i < 10000; i++) { l.set(i, new X); // throw some gets in there, too. if (i % 2 === 0) l.get(i / 2) } gc() var end = process.memoryUsage() t.equal(refs, startRefs, 'no leaky refs') console.error('start: %j\n' + 'end: %j', start, end); t.pass(); t.end(); })