lru-cache
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -43,5 +43,4 @@ ;(function () { // closure for web browsers | ||
var cache = {} // hash of items by key | ||
, lruList = {} // list of items in order of use recency | ||
, lru = 0 // least recently used | ||
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 | ||
@@ -71,12 +70,12 @@ , length = 0 // number of items in the list | ||
length = itemCount | ||
Object.keys(cache).forEach(function (key) { | ||
for (var key in cache) { | ||
cache[key].length = 1 | ||
}) | ||
} | ||
} else { | ||
lengthCalculator = lC | ||
length = 0 | ||
Object.keys(cache).forEach(function (key) { | ||
for (var key in cache) { | ||
cache[key].length = lengthCalculator(cache[key].value) | ||
length += cache[key].length | ||
}) | ||
} | ||
} | ||
@@ -103,9 +102,8 @@ | ||
if (dispose) { | ||
Object.keys(cache).forEach(function (k) { | ||
for (var k in cache) { | ||
dispose(k, cache[k].value) | ||
}) | ||
} | ||
} | ||
cache = {} | ||
lruList = {} | ||
lru = 0 | ||
mru = 0 | ||
@@ -141,3 +139,6 @@ length = 0 | ||
// oversized objects fall out of cache automatically. | ||
if (hit.length > max) return false | ||
if (hit.length > max) { | ||
if (dispose) dispose(key, value) | ||
return false | ||
} | ||
@@ -160,3 +161,2 @@ length += hit.length | ||
delete lruList[hit.lu] | ||
if (hit.lu === lru) lruWalk() | ||
hit.lu = mru ++ | ||
@@ -173,3 +173,2 @@ lruList[hit.lu] = hit | ||
delete lruList[hit.lu] | ||
if (hit.lu === lru) lruWalk() | ||
length -= hit.length | ||
@@ -179,21 +178,12 @@ itemCount -- | ||
function lruWalk () { | ||
// lru has been deleted, hop up to the next hit. | ||
for (var key in lruList) { | ||
return key | ||
} | ||
} | ||
function trim () { | ||
if (length <= max) return | ||
var prune = Object.keys(lruList) | ||
for (var i = 0; i < prune.length && length > max; i ++) { | ||
var hit = lruList[prune[i]] | ||
for (var k in lruList) { | ||
if (length <= max) break; | ||
var hit = lruList[k] | ||
if (dispose) dispose(hit.key, hit.value) | ||
length -= hit.length | ||
delete cache[ hit.key ] | ||
delete lruList[prune[i]] | ||
delete lruList[k] | ||
} | ||
lruWalk() | ||
} | ||
@@ -200,0 +190,0 @@ } |
{ | ||
"name": "lru-cache", | ||
"description": "A cache object that deletes the least-recently-used items.", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"author": "Isaac Z. Schlueter <i@izs.me>", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -249,1 +249,20 @@ var test = require("tap").test | ||
}) | ||
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() | ||
}) |
16428
8
389