Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2013-03-28 22:05:18 +0400
committerisaacs <i@izs.me>2013-03-28 22:05:18 +0400
commiteead166d15c09d423de6a50c8d6b96feb1294364 (patch)
treed4517651414d2320f8e4492ccc42d0fd9507c8fe /node_modules/lru-cache
parent60583077b9b810d141a788083439c950e2b70e29 (diff)
lru-cache@2.3.0
Diffstat (limited to 'node_modules/lru-cache')
-rw-r--r--node_modules/lru-cache/README.md12
-rw-r--r--node_modules/lru-cache/lib/lru-cache.js54
-rw-r--r--node_modules/lru-cache/package.json9
-rw-r--r--node_modules/lru-cache/s.js25
-rw-r--r--node_modules/lru-cache/test/basic.js28
5 files changed, 105 insertions, 23 deletions
diff --git a/node_modules/lru-cache/README.md b/node_modules/lru-cache/README.md
index ac48e49ac..03ee0f985 100644
--- a/node_modules/lru-cache/README.md
+++ b/node_modules/lru-cache/README.md
@@ -26,7 +26,8 @@ away.
## Options
-* `max` The maximum number of items. Not setting this is kind of
+* `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
@@ -59,6 +60,15 @@ away.
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.
diff --git a/node_modules/lru-cache/lib/lru-cache.js b/node_modules/lru-cache/lib/lru-cache.js
index b8fd86085..8c80853fd 100644
--- a/node_modules/lru-cache/lib/lru-cache.js
+++ b/node_modules/lru-cache/lib/lru-cache.js
@@ -48,6 +48,7 @@ function LRUCache (options) {
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
@@ -138,6 +139,7 @@ function LRUCache (options) {
}
cache = {}
lruList = {}
+ lru = 0
mru = 0
length = 0
itemCount = 0
@@ -191,38 +193,54 @@ function LRUCache (options) {
}
this.get = function (key) {
- if (!hOP(cache, key)) return
+ return get(key, true)
+ }
+
+ this.peek = function (key) {
+ return get(key, false)
+ }
+
+ function get (key, doUse) {
var hit = cache[key]
- if (maxAge && (Date.now() - hit.now > maxAge)) {
- this.del(key)
- return allowStale ? hit.value : undefined
+ 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
}
- delete lruList[hit.lu]
+ return hit
+ }
+
+ function use (hit) {
+ shiftLU(hit)
hit.lu = mru ++
lruList[hit.lu] = hit
- return hit.value
}
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]
- length -= hit.length
- itemCount --
+ del(cache[key])
}
function trim () {
- if (length <= max) return
- for (var k in lruList) {
- if (length <= max) break;
- var hit = lruList[k]
+ 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 ]
- delete lruList[k]
+ shiftLU(hit)
}
}
}
diff --git a/node_modules/lru-cache/package.json b/node_modules/lru-cache/package.json
index b0ee3f93a..d5a4e8c6d 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": "2.2.2",
+ "version": "2.3.0",
"author": {
"name": "Isaac Z. Schlueter",
"email": "i@izs.me"
@@ -15,7 +15,8 @@
"url": "git://github.com/isaacs/node-lru-cache.git"
},
"devDependencies": {
- "tap": ""
+ "tap": "",
+ "weak": ""
},
"license": {
"type": "MIT",
@@ -51,8 +52,8 @@
"email": "jesse.dailey@gmail.com"
}
],
- "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* `stale` By default, if you set a `maxAge`, it'll only actually pull\n stale items out of the cache when you `get(key)`. (That is, it's\n not pre-emptively doing a `setTimeout` or anything.) If you set\n `stale:true`, it'll return the stale value before deleting it. If\n you don't set this, then it'll return `undefined` when you try to\n get a stale entry, as if it had already been deleted.\n\n## API\n\n* `set(key, value)`\n* `get(key) => value`\n\n Both of these will update the \"recently used\"-ness of the key.\n They do what you think.\n\n* `del(key)`\n\n Deletes a key out of the cache.\n\n* `reset()`\n\n Clear the cache entirely, throwing away all values.\n\n* `has(key)`\n\n Check if a key is in the cache, without updating the recent-ness\n or deleting it for being stale.\n\n* `forEach(function(value,key,cache), [thisp])`\n\n Just like `Array.prototype.forEach`. Iterates over all the keys\n in the cache, in order of recent-ness. (Ie, more recently used\n items are iterated over first.)\n\n* `keys()`\n\n Return an array of the keys in the cache.\n\n* `values()`\n\n Return an array of the values in the cache.\n",
+ "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 size of the cache, checked by applying the length\n function to all values in the cache. 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* `stale` By default, if you set a `maxAge`, it'll only actually pull\n stale items out of the cache when you `get(key)`. (That is, it's\n not pre-emptively doing a `setTimeout` or anything.) If you set\n `stale:true`, it'll return the stale value before deleting it. If\n you don't set this, then it'll return `undefined` when you try to\n get a stale entry, as if it had already been deleted.\n\n## API\n\n* `set(key, value)`\n* `get(key) => value`\n\n Both of these will update the \"recently used\"-ness of the key.\n They do what you think.\n\n* `peek(key)`\n\n Returns the key value (or `undefined` if not found) without\n updating the \"recently used\"-ness of the key.\n\n (If you find yourself using this a lot, you *might* be using the\n wrong sort of data structure, but there are some use cases where\n it's handy.)\n\n* `del(key)`\n\n Deletes a key out of the cache.\n\n* `reset()`\n\n Clear the cache entirely, throwing away all values.\n\n* `has(key)`\n\n Check if a key is in the cache, without updating the recent-ness\n or deleting it for being stale.\n\n* `forEach(function(value,key,cache), [thisp])`\n\n Just like `Array.prototype.forEach`. Iterates over all the keys\n in the cache, in order of recent-ness. (Ie, more recently used\n items are iterated over first.)\n\n* `keys()`\n\n Return an array of the keys in the cache.\n\n* `values()`\n\n Return an array of the values in the cache.\n",
"readmeFilename": "README.md",
- "_id": "lru-cache@2.2.2",
+ "_id": "lru-cache@2.3.0",
"_from": "lru-cache@latest"
}
diff --git a/node_modules/lru-cache/s.js b/node_modules/lru-cache/s.js
new file mode 100644
index 000000000..c2a9e548f
--- /dev/null
+++ b/node_modules/lru-cache/s.js
@@ -0,0 +1,25 @@
+var LRU = require('lru-cache');
+
+var max = +process.argv[2] || 10240;
+var more = 1024;
+
+var cache = LRU({
+ max: max, maxAge: 86400e3
+});
+
+// fill cache
+for (var i = 0; i < max; ++i) {
+ cache.set(i, {});
+}
+
+var start = process.hrtime();
+
+// adding more items
+for ( ; i < max+more; ++i) {
+ cache.set(i, {});
+}
+
+var end = process.hrtime(start);
+var msecs = end[0] * 1E3 + end[1] / 1E6;
+
+console.log('adding %d items took %d ms', more, msecs.toPrecision(5));
diff --git a/node_modules/lru-cache/test/basic.js b/node_modules/lru-cache/test/basic.js
index dad13a894..70f3f8beb 100644
--- a/node_modules/lru-cache/test/basic.js
+++ b/node_modules/lru-cache/test/basic.js
@@ -299,3 +299,31 @@ test("stale", function(t) {
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()
+})