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
var LRU = require("lru-cache")
, cache = minimatch.cache = new LRU(100)
, cache = minimatch.cache = new LRU({max: 100})
, GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
var path = require("path")

View file

@ -3,3 +3,5 @@ Isaac Z. Schlueter <i@izs.me>
Carlos Brito Lage <carlos@carloslage.net>
Marko Mikulicic <marko.mikulicic@isti.cnr.it>
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.
Usage:
## Usage:
var LRU = require("lru-cache")
, cache = LRU(10, // max length. default = Infinity
// 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 })
```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.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.

View file

@ -4,7 +4,7 @@ if (module) {
module.exports = LRUCache
} else {
// just set the global for non-node platforms.
;(function () { return this })().LRUCache = LRUCache
this.LRUCache = LRUCache
}
function hOP (obj, key) {
@ -13,18 +13,32 @@ function hOP (obj, key) {
function naiveLength () { return 1 }
function LRUCache (maxLength, lengthCalculator) {
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 = {}
var 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) {
, 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) {
})
}
if (length > maxLength) trim()
if (length > max) trim()
}
, get : function () { return lengthCalculator }
, enumerable : true
@ -83,6 +97,11 @@ function LRUCache (maxLength, lengthCalculator) {
})
this.reset = function () {
if (dispose) {
Object.keys(cache).forEach(function (k) {
dispose(k, cache[k].value)
})
}
cache = {}
lruList = {}
lru = 0
@ -99,26 +118,40 @@ function LRUCache (maxLength, lengthCalculator) {
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
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.
if (hit.length > maxLength) return
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)) {
this.del(key)
return
}
delete lruList[hit.lu]
if (hit.lu === lru) lruWalk()
hit.lu = mru ++
@ -129,6 +162,7 @@ function LRUCache (maxLength, lengthCalculator) {
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()
@ -142,13 +176,16 @@ function LRUCache (maxLength, lengthCalculator) {
}
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]]
}
lruWalk()
}
}

View file

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

View file

@ -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")
@ -169,3 +181,68 @@ test("lru recently gotten with weighed length", function (t) {
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()
})

24
node_modules/minimatch/package.json generated vendored

File diff suppressed because one or more lines are too long