lru-cache
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -7,3 +7,3 @@ ;(function () { // closure for web browsers | ||
// just set the global for non-node platforms. | ||
;(function () { return this })().LRUCache = LRUCache | ||
this.LRUCache = LRUCache | ||
} | ||
@@ -17,3 +17,3 @@ | ||
function LRUCache (maxLength, lengthCalculator) { | ||
function LRUCache (maxLength, lengthCalculator, maxAge) { | ||
if (!(this instanceof LRUCache)) { | ||
@@ -106,9 +106,10 @@ return new LRUCache(maxLength, lengthCalculator) | ||
cache[key].value = value | ||
return | ||
return true | ||
} | ||
var hit = {key:key, value:value, lu:mru++, length:lengthCalculator(value)} | ||
if (maxAge) hit.now = Date.now() | ||
// oversized objects fall out of cache automatically. | ||
if (hit.length > maxLength) return | ||
if (hit.length > maxLength) return false | ||
@@ -120,2 +121,3 @@ length += hit.length | ||
if (length > maxLength) trim() | ||
return true | ||
} | ||
@@ -126,2 +128,6 @@ | ||
var hit = cache[key] | ||
if (maxAge && Date.now() - hit.now > maxAge) { | ||
this.del(key) | ||
return | ||
} | ||
delete lruList[hit.lu] | ||
@@ -157,2 +163,3 @@ if (hit.lu === lru) lruWalk() | ||
} | ||
lruWalk() | ||
@@ -159,0 +166,0 @@ } |
@@ -1,13 +0,18 @@ | ||
{ "name": "lru-cache" | ||
, "description": "A cache object that deletes the least-recently-used items." | ||
, "version": "1.1.0" | ||
, "author": "Isaac Z. Schlueter <i@izs.me>" | ||
, "scripts": { "test": "tap test" } | ||
, "main": "lib/lru-cache.js" | ||
, "repository": "git://github.com/isaacs/node-lru-cache.git" | ||
, "devDependencies": { "tap": "" } | ||
, "license": | ||
{ "type": "MIT" | ||
, "url": "http://github.com/isaacs/node-lru-cache/raw/master/LICENSE" | ||
{ | ||
"name": "lru-cache", | ||
"description": "A cache object that deletes the least-recently-used items.", | ||
"version": "1.1.1", | ||
"author": "Isaac Z. Schlueter <i@izs.me>", | ||
"scripts": { | ||
"test": "tap test" | ||
}, | ||
"main": "lib/lru-cache.js", | ||
"repository": "git://github.com/isaacs/node-lru-cache.git", | ||
"devDependencies": { | ||
"tap": "" | ||
}, | ||
"license": { | ||
"type": "MIT", | ||
"url": "http://github.com/isaacs/node-lru-cache/raw/master/LICENSE" | ||
} | ||
} |
@@ -9,2 +9,3 @@ # lru cache | ||
, cache = LRU(10, // max length. default = Infinity | ||
// calculate how "big" each item is | ||
@@ -15,4 +16,10 @@ // | ||
// relative size. | ||
function (item) { return item.length }) | ||
function (item) { return item.length }, | ||
// 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") | ||
@@ -19,0 +26,0 @@ cache.get("key") // "value" |
@@ -172,1 +172,40 @@ var test = require("tap").test | ||
}) | ||
test("set returns proper booleans", function(t) { | ||
var cache = new LRU(5, function (item) { return item.length }) | ||
t.equal(cache.set("a", "A"), true) | ||
// should return false for maxLength 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(5, null, 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")) | ||
}, 51) | ||
setTimeout(function () { | ||
t.notOk(cache.get("b")) | ||
t.equal(cache.get("c"), "C") | ||
}, 90) | ||
setTimeout(function () { | ||
t.notOk(cache.get("c")) | ||
t.end() | ||
}, 155) | ||
}) |
Sorry, the diff of this file is not supported yet
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
11934
317
34
1