update the bundled minimatch to v0.2.6

This commit is contained in:
Nathan Rajlich 2012-08-21 19:06:26 -07:00
parent 92ec0d995b
commit 9e2cf8979e
7 changed files with 208 additions and 89 deletions

View file

@ -30,7 +30,7 @@ if (!require) {
minimatch.Minimatch = Minimatch minimatch.Minimatch = Minimatch
var LRU = require("lru-cache") var LRU = require("lru-cache")
, cache = minimatch.cache = new LRU(100) , cache = minimatch.cache = new LRU({max: 100})
, GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {} , GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
var path = require("path") var path = require("path")

View file

@ -3,3 +3,5 @@ Isaac Z. Schlueter <i@izs.me>
Carlos Brito Lage <carlos@carloslage.net> Carlos Brito Lage <carlos@carloslage.net>
Marko Mikulicic <marko.mikulicic@isti.cnr.it> Marko Mikulicic <marko.mikulicic@isti.cnr.it>
Trent Mick <trentm@gmail.com> Trent Mick <trentm@gmail.com>
Kevin O'Hara <kevinohara80@gmail.com>
Marco Rogers <marco.rogers@gmail.com>

View file

@ -2,25 +2,45 @@
A cache object that deletes the least-recently-used items. A cache object that deletes the least-recently-used items.
Usage: ## Usage:
var LRU = require("lru-cache") ```javascript
, cache = LRU(10, // max length. default = Infinity var LRU = require("lru-cache")
// calculate how "big" each item is , options = { max: 500
// , length: function (n) { return n * 2 }
// defaults to function(){return 1}, ie, just limit , dispose: function (key, n) { n.close() }
// the item count, without any knowledge as to their , maxAge: 1000 * 60 * 60 }
// relative size. , cache = LRU(options)
function (item) { return item.length }) , otherCache = LRU(50) // sets just the max size
cache.set("key", "value") cache.set("key", "value")
cache.get("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 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 If you try to put an oversized thing in it, then it'll fall out right
away. 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.

View file

@ -4,7 +4,7 @@ if (module) {
module.exports = LRUCache module.exports = LRUCache
} else { } else {
// just set the global for non-node platforms. // just set the global for non-node platforms.
;(function () { return this })().LRUCache = LRUCache this.LRUCache = LRUCache
} }
function hOP (obj, key) { function hOP (obj, key) {
@ -13,18 +13,32 @@ function hOP (obj, key) {
function naiveLength () { return 1 } function naiveLength () { return 1 }
function LRUCache (maxLength, lengthCalculator) { function LRUCache (options) {
if (!(this instanceof LRUCache)) { 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 = {}
var lengthCalculator = options.length || naiveLength
if (typeof lengthCalculator !== "function") { if (typeof lengthCalculator !== "function") {
lengthCalculator = naiveLength lengthCalculator = naiveLength
} }
if (!maxLength || !(typeof maxLength === "number") || maxLength <= 0 ) { if (!max || !(typeof max === "number") || max <= 0 ) {
maxLength = Infinity max = Infinity
} }
var maxAge = options.maxAge || null
var dispose = options.dispose
var cache = {} // hash of items by key var cache = {} // hash of items by key
, lruList = {} // list of items in order of use recency , lruList = {} // list of items in order of use recency
@ -34,16 +48,16 @@ function LRUCache (maxLength, lengthCalculator) {
, itemCount = 0 , itemCount = 0
// resize the cache when the maxLength changes. // resize the cache when the max changes.
Object.defineProperty(this, "maxLength", Object.defineProperty(this, "max",
{ set : function (mL) { { set : function (mL) {
if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity
maxLength = mL max = mL
// if it gets above double maxLength, trim right away. // if it gets above double max, trim right away.
// otherwise, do it whenever it's convenient. // 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 , enumerable : true
}) })
@ -65,7 +79,7 @@ function LRUCache (maxLength, lengthCalculator) {
}) })
} }
if (length > maxLength) trim() if (length > max) trim()
} }
, get : function () { return lengthCalculator } , get : function () { return lengthCalculator }
, enumerable : true , enumerable : true
@ -83,6 +97,11 @@ function LRUCache (maxLength, lengthCalculator) {
}) })
this.reset = function () { this.reset = function () {
if (dispose) {
Object.keys(cache).forEach(function (k) {
dispose(k, cache[k].value)
})
}
cache = {} cache = {}
lruList = {} lruList = {}
lru = 0 lru = 0
@ -99,26 +118,40 @@ function LRUCache (maxLength, lengthCalculator) {
this.set = function (key, value) { this.set = function (key, value) {
if (hOP(cache, key)) { 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 cache[key].value = value
return this.get(key)
return true
} }
var hit = {key:key, value:value, lu:mru++, length:lengthCalculator(value)} var hit = {
key:key,
value:value,
lu:mru++,
length:lengthCalculator(value),
now: (maxAge) ? Date.now() : 0
}
// oversized objects fall out of cache automatically. // oversized objects fall out of cache automatically.
if (hit.length > maxLength) return if (hit.length > max) return false
length += hit.length length += hit.length
lruList[hit.lu] = cache[key] = hit lruList[hit.lu] = cache[key] = hit
itemCount ++ itemCount ++
if (length > maxLength) trim() if (length > max) trim()
return true
} }
this.get = function (key) { this.get = function (key) {
if (!hOP(cache, key)) return if (!hOP(cache, key)) return
var hit = cache[key] var hit = cache[key]
if (maxAge && (Date.now() - hit.now > maxAge)) {
this.del(key)
return
}
delete lruList[hit.lu] delete lruList[hit.lu]
if (hit.lu === lru) lruWalk() if (hit.lu === lru) lruWalk()
hit.lu = mru ++ hit.lu = mru ++
@ -129,6 +162,7 @@ function LRUCache (maxLength, lengthCalculator) {
this.del = function (key) { this.del = function (key) {
if (!hOP(cache, key)) return if (!hOP(cache, key)) return
var hit = cache[key] var hit = cache[key]
if (dispose) dispose(key, hit.value)
delete cache[key] delete cache[key]
delete lruList[hit.lu] delete lruList[hit.lu]
if (hit.lu === lru) lruWalk() if (hit.lu === lru) lruWalk()
@ -142,13 +176,16 @@ function LRUCache (maxLength, lengthCalculator) {
} }
function trim () { function trim () {
if (length <= maxLength) return if (length <= max) return
var prune = Object.keys(lruList) var prune = Object.keys(lruList)
for (var i = 0; i < prune.length && length > maxLength; i ++) { for (var i = 0; i < prune.length && length > max; i ++) {
length -= lruList[prune[i]].length var hit = lruList[prune[i]]
delete cache[ lruList[prune[i]].key ] if (dispose) dispose(hit.key, hit.value)
length -= hit.length
delete cache[ hit.key ]
delete lruList[prune[i]] delete lruList[prune[i]]
} }
lruWalk() lruWalk()
} }
} }

View file

@ -1,7 +1,7 @@
{ {
"name": "lru-cache", "name": "lru-cache",
"description": "A cache object that deletes the least-recently-used items.", "description": "A cache object that deletes the least-recently-used items.",
"version": "1.1.0", "version": "2.0.1",
"author": { "author": {
"name": "Isaac Z. Schlueter", "name": "Isaac Z. Schlueter",
"email": "i@izs.me" "email": "i@izs.me"
@ -21,11 +21,6 @@
"type": "MIT", "type": "MIT",
"url": "http://github.com/isaacs/node-lru-cache/raw/master/LICENSE" "url": "http://github.com/isaacs/node-lru-cache/raw/master/LICENSE"
}, },
"_npmUser": {
"name": "tootallnate",
"email": "nathan@tootallnate.net"
},
"_id": "lru-cache@1.1.0",
"contributors": [ "contributors": [
{ {
"name": "Isaac Z. Schlueter", "name": "Isaac Z. Schlueter",
@ -42,19 +37,17 @@
{ {
"name": "Trent Mick", "name": "Trent Mick",
"email": "trentm@gmail.com" "email": "trentm@gmail.com"
},
{
"name": "Kevin O'Hara",
"email": "kevinohara80@gmail.com"
},
{
"name": "Marco Rogers",
"email": "marco.rogers@gmail.com"
} }
], ],
"dependencies": {}, "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",
"optionalDependencies": {}, "_id": "lru-cache@2.0.1",
"engines": { "_from": "lru-cache@~2.0.0"
"node": "*"
},
"_engineSupported": true,
"_npmVersion": "1.1.18",
"_nodeVersion": "v0.6.18",
"_defaultsLoaded": true,
"dist": {
"shasum": "d04716a22fc9499aaec8d70ab63e04be307bc4ea"
},
"_from": "lru-cache@~1"
} }

View file

@ -2,12 +2,12 @@ var test = require("tap").test
, LRU = require("../") , LRU = require("../")
test("basic", function (t) { test("basic", function (t) {
var cache = new LRU(10) var cache = new LRU({max: 10})
cache.set("key", "value") cache.set("key", "value")
t.equal(cache.get("key"), "value") t.equal(cache.get("key"), "value")
t.equal(cache.get("nada"), undefined) t.equal(cache.get("nada"), undefined)
t.equal(cache.length, 1) t.equal(cache.length, 1)
t.equal(cache.maxLength, 10) t.equal(cache.max, 10)
t.end() t.end()
}) })
@ -42,17 +42,17 @@ test("del", function (t) {
t.end() t.end()
}) })
test("maxLength", function (t) { test("max", function (t) {
var cache = new LRU(3) var cache = new LRU(3)
// test changing the maxLength, verify that the LRU items get dropped. // test changing the max, verify that the LRU items get dropped.
cache.maxLength = 100 cache.max = 100
for (var i = 0; i < 100; i ++) cache.set(i, i) for (var i = 0; i < 100; i ++) cache.set(i, i)
t.equal(cache.length, 100) t.equal(cache.length, 100)
for (var i = 0; i < 100; i ++) { for (var i = 0; i < 100; i ++) {
t.equal(cache.get(i), i) t.equal(cache.get(i), i)
} }
cache.maxLength = 3 cache.max = 3
t.equal(cache.length, 3) t.equal(cache.length, 3)
for (var i = 0; i < 97; i ++) { for (var i = 0; i < 97; i ++) {
t.equal(cache.get(i), undefined) t.equal(cache.get(i), undefined)
@ -61,15 +61,15 @@ test("maxLength", function (t) {
t.equal(cache.get(i), i) t.equal(cache.get(i), i)
} }
// now remove the maxLength restriction, and try again. // now remove the max restriction, and try again.
cache.maxLength = "hello" cache.max = "hello"
for (var i = 0; i < 100; i ++) cache.set(i, i) for (var i = 0; i < 100; i ++) cache.set(i, i)
t.equal(cache.length, 100) t.equal(cache.length, 100)
for (var i = 0; i < 100; i ++) { for (var i = 0; i < 100; i ++) {
t.equal(cache.get(i), i) t.equal(cache.get(i), i)
} }
// should trigger an immediate resize // should trigger an immediate resize
cache.maxLength = 3 cache.max = 3
t.equal(cache.length, 3) t.equal(cache.length, 3)
for (var i = 0; i < 97; i ++) { for (var i = 0; i < 97; i ++) {
t.equal(cache.get(i), undefined) t.equal(cache.get(i), undefined)
@ -86,7 +86,7 @@ test("reset", function (t) {
cache.set("b", "B") cache.set("b", "B")
cache.reset() cache.reset()
t.equal(cache.length, 0) 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("a"), undefined)
t.equal(cache.get("b"), undefined) t.equal(cache.get("b"), undefined)
t.end() t.end()
@ -119,20 +119,26 @@ test("dump", function (t) {
test("basic with weighed length", 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}) cache.set("key", {val: "value", size: 50})
t.equal(cache.get("key").val, "value") t.equal(cache.get("key").val, "value")
t.equal(cache.get("nada"), undefined) t.equal(cache.get("nada"), undefined)
t.equal(cache.lengthCalculator(cache.get("key")), 50) t.equal(cache.lengthCalculator(cache.get("key")), 50)
t.equal(cache.length, 50) t.equal(cache.length, 50)
t.equal(cache.maxLength, 100) t.equal(cache.max, 100)
t.end() t.end()
}) })
test("weighed length item too large", function (t) { test("weighed length item too large", function (t) {
var cache = new LRU(10, function (item) { return item.size } ) var cache = new LRU({
t.equal(cache.maxLength, 10) max: 10,
length: function (item) { return item.size }
})
t.equal(cache.max, 10)
// should fall out immediately // should fall out immediately
cache.set("key", {val: "value", size: 50}) 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) { 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("a", "A")
cache.set("b", "BB") cache.set("b", "BB")
cache.set("c", "CCC") 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) { 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("a", "A")
cache.set("b", "BB") cache.set("b", "BB")
cache.set("c", "CCC") cache.set("c", "CCC")
@ -169,3 +181,68 @@ test("lru recently gotten with weighed length", function (t) {
t.equal(cache.get("a"), "A") t.equal(cache.get("a"), "A")
t.end() 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()
})

24
node_modules/minimatch/package.json generated vendored

File diff suppressed because one or more lines are too long