lru-cache
Advanced tools
Comparing version 2.6.5 to 2.7.0
@@ -140,6 +140,20 @@ ;(function () { // closure for web browsers | ||
// Provided for debugging/dev purposes only. No promises whatsoever that | ||
// this API stays stable. | ||
LRUCache.prototype.dump = function () { | ||
return this._cache | ||
var arr = [] | ||
var i = 0 | ||
for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) { | ||
var hit = this._lruList[k] | ||
if (!isStale(this, hit)) { | ||
//Do not store staled hits | ||
++i | ||
arr.push({ | ||
k: hit.key, | ||
v: hit.value, | ||
e: hit.now + (hit.maxAge || 0) | ||
}); | ||
} | ||
} | ||
//arr has the most read first | ||
return arr | ||
} | ||
@@ -154,4 +168,9 @@ | ||
var now = maxAge ? Date.now() : 0 | ||
var len = this._lengthCalculator(value) | ||
if (hOP(this._cache, key)) { | ||
if (len > this._max) { | ||
del(this, this._cache[key]) | ||
return false | ||
} | ||
// dispose of the old one before overwriting | ||
@@ -164,7 +183,12 @@ if (this._dispose) | ||
this._cache[key].value = value | ||
this._length += (len - this._cache[key].length) | ||
this._cache[key].length = len | ||
this.get(key) | ||
if (this._length > this._max) | ||
trim(this) | ||
return true | ||
} | ||
var len = this._lengthCalculator(value) | ||
var hit = new Entry(key, value, this._mru++, len, now, maxAge) | ||
@@ -215,2 +239,22 @@ | ||
LRUCache.prototype.load = function (arr) { | ||
//reset the cache | ||
this.reset(); | ||
var now = Date.now() | ||
//A previous serialized cache has the most recent items first | ||
for (var l = arr.length - 1; l >= 0; l-- ) { | ||
var hit = arr[l] | ||
var expiresAt = hit.e || 0 | ||
if (expiresAt === 0) { | ||
//the item was created without expiration in a non aged cache | ||
this.set(hit.k, hit.v) | ||
} else { | ||
var maxAge = expiresAt - now | ||
//dont add already expired items | ||
if (maxAge > 0) this.set(hit.k, hit.v, maxAge) | ||
} | ||
} | ||
} | ||
function get (self, key, doUse) { | ||
@@ -217,0 +261,0 @@ var hit = self._cache[key] |
{ | ||
"name": "lru-cache", | ||
"description": "A cache object that deletes the least-recently-used items.", | ||
"version": "2.6.5", | ||
"version": "2.7.0", | ||
"author": "Isaac Z. Schlueter <i@izs.me>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -39,3 +39,3 @@ # lru cache | ||
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` | ||
`function(n){return 1}`, which is fine if you want to store `max` | ||
like-sized things. | ||
@@ -106,3 +106,3 @@ * `dispose` Function that is called on items when they are dropped | ||
* `itemCount()` | ||
* `itemCount` | ||
@@ -112,1 +112,11 @@ Return total quantity of objects currently in cache. Note, that | ||
count. | ||
* `dump()` | ||
Return an array of the cache entries ready for serialization and usage | ||
with 'destinationCache.load(arr)`. | ||
* `load(cacheEntriesArray)` | ||
Loads another cache entries array, obtained with `sourceCache.dump()`, | ||
into the cache. The destination cache is reset before loading new entries |
@@ -96,27 +96,2 @@ var test = require("tap").test | ||
// Note: `<cache>.dump()` is a debugging tool only. No guarantees are made | ||
// about the format/layout of the response. | ||
test("dump", function (t) { | ||
var cache = new LRU(10) | ||
var d = cache.dump(); | ||
t.equal(Object.keys(d).length, 0, "nothing in dump for empty cache") | ||
cache.set("a", "A") | ||
var d = cache.dump() // { a: { key: "a", value: "A", lu: 0 } } | ||
t.ok(d.a) | ||
t.equal(d.a.key, "a") | ||
t.equal(d.a.value, "A") | ||
t.equal(d.a.lu, 0) | ||
cache.set("b", "B") | ||
cache.get("b") | ||
d = cache.dump() | ||
t.ok(d.b) | ||
t.equal(d.b.key, "b") | ||
t.equal(d.b.value, "B") | ||
t.equal(d.b.lu, 2) | ||
t.end() | ||
}) | ||
test("basic with weighed length", function (t) { | ||
@@ -186,2 +161,28 @@ var cache = new LRU({ | ||
test("lru recently updated with weighed length", function (t) { | ||
var cache = new LRU({ | ||
max: 8, | ||
length: function (item) { return item.length } | ||
}) | ||
cache.set("a", "A") | ||
cache.set("b", "BB") | ||
cache.set("c", "CCC") | ||
t.equal(cache.length, 6) //CCC BB A | ||
cache.set("a", "+A") | ||
t.equal(cache.length, 7) //+A CCC BB | ||
cache.set("b", "++BB") | ||
t.equal(cache.length, 6) //++BB +A | ||
t.equal(cache.get("c"), undefined) | ||
cache.set("c", "oversized") | ||
t.equal(cache.length, 6) //++BB +A | ||
t.equal(cache.get("c"), undefined) | ||
cache.set("a", "oversized") | ||
t.equal(cache.length, 4) //++BB | ||
t.equal(cache.get("a"), undefined) | ||
t.equal(cache.get("b"), "++BB") | ||
t.end() | ||
}) | ||
test("set returns proper booleans", function(t) { | ||
@@ -188,0 +189,0 @@ var cache = new LRU({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29586
11
922
120