lru-cache
Advanced tools
Comparing version 4.1.0 to 4.1.1
181
index.js
@@ -0,1 +1,3 @@ | ||
'use strict' | ||
module.exports = LRUCache | ||
@@ -12,3 +14,2 @@ | ||
// use symbols if possible, otherwise just _props | ||
var symbols = {} | ||
var hasSymbol = typeof Symbol === 'function' | ||
@@ -26,17 +27,11 @@ var makeSymbol | ||
function priv (obj, key, val) { | ||
var sym | ||
if (symbols[key]) { | ||
sym = symbols[key] | ||
} else { | ||
sym = makeSymbol(key) | ||
symbols[key] = sym | ||
} | ||
if (arguments.length === 2) { | ||
return obj[sym] | ||
} else { | ||
obj[sym] = val | ||
return val | ||
} | ||
} | ||
var MAX = makeSymbol('max') | ||
var LENGTH = makeSymbol('length') | ||
var LENGTH_CALCULATOR = makeSymbol('lengthCalculator') | ||
var ALLOW_STALE = makeSymbol('allowStale') | ||
var MAX_AGE = makeSymbol('maxAge') | ||
var DISPOSE = makeSymbol('dispose') | ||
var NO_DISPOSE_ON_SET = makeSymbol('noDisposeOnSet') | ||
var LRU_LIST = makeSymbol('lruList') | ||
var CACHE = makeSymbol('cache') | ||
@@ -66,3 +61,3 @@ function naiveLength () { return 1 } | ||
var max = priv(this, 'max', options.max) | ||
var max = this[MAX] = options.max | ||
// Kind of weird to have a default max of Infinity, but oh well. | ||
@@ -72,3 +67,3 @@ if (!max || | ||
max <= 0) { | ||
priv(this, 'max', Infinity) | ||
this[MAX] = Infinity | ||
} | ||
@@ -80,8 +75,8 @@ | ||
} | ||
priv(this, 'lengthCalculator', lc) | ||
this[LENGTH_CALCULATOR] = lc | ||
priv(this, 'allowStale', options.stale || false) | ||
priv(this, 'maxAge', options.maxAge || 0) | ||
priv(this, 'dispose', options.dispose) | ||
priv(this, 'noDisposeOnSet', options.noDisposeOnSet || false) | ||
this[ALLOW_STALE] = options.stale || false | ||
this[MAX_AGE] = options.maxAge || 0 | ||
this[DISPOSE] = options.dispose | ||
this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false | ||
this.reset() | ||
@@ -96,7 +91,7 @@ } | ||
} | ||
priv(this, 'max', mL) | ||
this[MAX] = mL | ||
trim(this) | ||
}, | ||
get: function () { | ||
return priv(this, 'max') | ||
return this[MAX] | ||
}, | ||
@@ -108,6 +103,6 @@ enumerable: true | ||
set: function (allowStale) { | ||
priv(this, 'allowStale', !!allowStale) | ||
this[ALLOW_STALE] = !!allowStale | ||
}, | ||
get: function () { | ||
return priv(this, 'allowStale') | ||
return this[ALLOW_STALE] | ||
}, | ||
@@ -122,7 +117,7 @@ enumerable: true | ||
} | ||
priv(this, 'maxAge', mA) | ||
this[MAX_AGE] = mA | ||
trim(this) | ||
}, | ||
get: function () { | ||
return priv(this, 'maxAge') | ||
return this[MAX_AGE] | ||
}, | ||
@@ -138,8 +133,8 @@ enumerable: true | ||
} | ||
if (lC !== priv(this, 'lengthCalculator')) { | ||
priv(this, 'lengthCalculator', lC) | ||
priv(this, 'length', 0) | ||
priv(this, 'lruList').forEach(function (hit) { | ||
hit.length = priv(this, 'lengthCalculator').call(this, hit.value, hit.key) | ||
priv(this, 'length', priv(this, 'length') + hit.length) | ||
if (lC !== this[LENGTH_CALCULATOR]) { | ||
this[LENGTH_CALCULATOR] = lC | ||
this[LENGTH] = 0 | ||
this[LRU_LIST].forEach(function (hit) { | ||
hit.length = this[LENGTH_CALCULATOR](hit.value, hit.key) | ||
this[LENGTH] += hit.length | ||
}, this) | ||
@@ -149,3 +144,3 @@ } | ||
}, | ||
get: function () { return priv(this, 'lengthCalculator') }, | ||
get: function () { return this[LENGTH_CALCULATOR] }, | ||
enumerable: true | ||
@@ -155,3 +150,3 @@ }) | ||
Object.defineProperty(LRUCache.prototype, 'length', { | ||
get: function () { return priv(this, 'length') }, | ||
get: function () { return this[LENGTH] }, | ||
enumerable: true | ||
@@ -161,3 +156,3 @@ }) | ||
Object.defineProperty(LRUCache.prototype, 'itemCount', { | ||
get: function () { return priv(this, 'lruList').length }, | ||
get: function () { return this[LRU_LIST].length }, | ||
enumerable: true | ||
@@ -168,3 +163,3 @@ }) | ||
thisp = thisp || this | ||
for (var walker = priv(this, 'lruList').tail; walker !== null;) { | ||
for (var walker = this[LRU_LIST].tail; walker !== null;) { | ||
var prev = walker.prev | ||
@@ -180,3 +175,3 @@ forEachStep(this, fn, walker, thisp) | ||
del(self, node) | ||
if (!priv(self, 'allowStale')) { | ||
if (!self[ALLOW_STALE]) { | ||
hit = undefined | ||
@@ -192,3 +187,3 @@ } | ||
thisp = thisp || this | ||
for (var walker = priv(this, 'lruList').head; walker !== null;) { | ||
for (var walker = this[LRU_LIST].head; walker !== null;) { | ||
var next = walker.next | ||
@@ -201,3 +196,3 @@ forEachStep(this, fn, walker, thisp) | ||
LRUCache.prototype.keys = function () { | ||
return priv(this, 'lruList').toArray().map(function (k) { | ||
return this[LRU_LIST].toArray().map(function (k) { | ||
return k.key | ||
@@ -208,3 +203,3 @@ }, this) | ||
LRUCache.prototype.values = function () { | ||
return priv(this, 'lruList').toArray().map(function (k) { | ||
return this[LRU_LIST].toArray().map(function (k) { | ||
return k.value | ||
@@ -215,17 +210,17 @@ }, this) | ||
LRUCache.prototype.reset = function () { | ||
if (priv(this, 'dispose') && | ||
priv(this, 'lruList') && | ||
priv(this, 'lruList').length) { | ||
priv(this, 'lruList').forEach(function (hit) { | ||
priv(this, 'dispose').call(this, hit.key, hit.value) | ||
if (this[DISPOSE] && | ||
this[LRU_LIST] && | ||
this[LRU_LIST].length) { | ||
this[LRU_LIST].forEach(function (hit) { | ||
this[DISPOSE](hit.key, hit.value) | ||
}, this) | ||
} | ||
priv(this, 'cache', new Map()) // hash of items by key | ||
priv(this, 'lruList', new Yallist()) // list of items in order of use recency | ||
priv(this, 'length', 0) // length of items in the list | ||
this[CACHE] = new Map() // hash of items by key | ||
this[LRU_LIST] = new Yallist() // list of items in order of use recency | ||
this[LENGTH] = 0 // length of items in the list | ||
} | ||
LRUCache.prototype.dump = function () { | ||
return priv(this, 'lruList').map(function (hit) { | ||
return this[LRU_LIST].map(function (hit) { | ||
if (!isStale(this, hit)) { | ||
@@ -244,3 +239,3 @@ return { | ||
LRUCache.prototype.dumpLru = function () { | ||
return priv(this, 'lruList') | ||
return this[LRU_LIST] | ||
} | ||
@@ -252,3 +247,3 @@ | ||
var as = priv(this, 'allowStale') | ||
var as = this[ALLOW_STALE] | ||
if (as) { | ||
@@ -259,3 +254,3 @@ str += '\n allowStale: true' | ||
var max = priv(this, 'max') | ||
var max = this[MAX] | ||
if (max && max !== Infinity) { | ||
@@ -269,3 +264,3 @@ if (extras) { | ||
var maxAge = priv(this, 'maxAge') | ||
var maxAge = this[MAX_AGE] | ||
if (maxAge) { | ||
@@ -279,3 +274,3 @@ if (extras) { | ||
var lc = priv(this, 'lengthCalculator') | ||
var lc = this[LENGTH_CALCULATOR] | ||
if (lc && lc !== naiveLength) { | ||
@@ -285,3 +280,3 @@ if (extras) { | ||
} | ||
str += '\n length: ' + util.inspect(priv(this, 'length'), opts) | ||
str += '\n length: ' + util.inspect(this[LENGTH], opts) | ||
extras = true | ||
@@ -291,3 +286,3 @@ } | ||
var didFirst = false | ||
priv(this, 'lruList').forEach(function (item) { | ||
this[LRU_LIST].forEach(function (item) { | ||
if (didFirst) { | ||
@@ -327,14 +322,14 @@ str += ',\n ' | ||
LRUCache.prototype.set = function (key, value, maxAge) { | ||
maxAge = maxAge || priv(this, 'maxAge') | ||
maxAge = maxAge || this[MAX_AGE] | ||
var now = maxAge ? Date.now() : 0 | ||
var len = priv(this, 'lengthCalculator').call(this, value, key) | ||
var len = this[LENGTH_CALCULATOR](value, key) | ||
if (priv(this, 'cache').has(key)) { | ||
if (len > priv(this, 'max')) { | ||
del(this, priv(this, 'cache').get(key)) | ||
if (this[CACHE].has(key)) { | ||
if (len > this[MAX]) { | ||
del(this, this[CACHE].get(key)) | ||
return false | ||
} | ||
var node = priv(this, 'cache').get(key) | ||
var node = this[CACHE].get(key) | ||
var item = node.value | ||
@@ -344,5 +339,5 @@ | ||
// split out into 2 ifs for better coverage tracking | ||
if (priv(this, 'dispose')) { | ||
if (!priv(this, 'noDisposeOnSet')) { | ||
priv(this, 'dispose').call(this, key, item.value) | ||
if (this[DISPOSE]) { | ||
if (!this[NO_DISPOSE_ON_SET]) { | ||
this[DISPOSE](key, item.value) | ||
} | ||
@@ -354,3 +349,3 @@ } | ||
item.value = value | ||
priv(this, 'length', priv(this, 'length') + (len - item.length)) | ||
this[LENGTH] += len - item.length | ||
item.length = len | ||
@@ -365,5 +360,5 @@ this.get(key) | ||
// oversized objects fall out of cache automatically. | ||
if (hit.length > priv(this, 'max')) { | ||
if (priv(this, 'dispose')) { | ||
priv(this, 'dispose').call(this, key, value) | ||
if (hit.length > this[MAX]) { | ||
if (this[DISPOSE]) { | ||
this[DISPOSE](key, value) | ||
} | ||
@@ -373,5 +368,5 @@ return false | ||
priv(this, 'length', priv(this, 'length') + hit.length) | ||
priv(this, 'lruList').unshift(hit) | ||
priv(this, 'cache').set(key, priv(this, 'lruList').head) | ||
this[LENGTH] += hit.length | ||
this[LRU_LIST].unshift(hit) | ||
this[CACHE].set(key, this[LRU_LIST].head) | ||
trim(this) | ||
@@ -382,4 +377,4 @@ return true | ||
LRUCache.prototype.has = function (key) { | ||
if (!priv(this, 'cache').has(key)) return false | ||
var hit = priv(this, 'cache').get(key).value | ||
if (!this[CACHE].has(key)) return false | ||
var hit = this[CACHE].get(key).value | ||
if (isStale(this, hit)) { | ||
@@ -400,3 +395,3 @@ return false | ||
LRUCache.prototype.pop = function () { | ||
var node = priv(this, 'lruList').tail | ||
var node = this[LRU_LIST].tail | ||
if (!node) return null | ||
@@ -408,3 +403,3 @@ del(this, node) | ||
LRUCache.prototype.del = function (key) { | ||
del(this, priv(this, 'cache').get(key)) | ||
del(this, this[CACHE].get(key)) | ||
} | ||
@@ -436,3 +431,3 @@ | ||
var self = this | ||
priv(this, 'cache').forEach(function (value, key) { | ||
this[CACHE].forEach(function (value, key) { | ||
get(self, key, false) | ||
@@ -443,3 +438,3 @@ }) | ||
function get (self, key, doUse) { | ||
var node = priv(self, 'cache').get(key) | ||
var node = self[CACHE].get(key) | ||
if (node) { | ||
@@ -449,6 +444,6 @@ var hit = node.value | ||
del(self, node) | ||
if (!priv(self, 'allowStale')) hit = undefined | ||
if (!self[ALLOW_STALE]) hit = undefined | ||
} else { | ||
if (doUse) { | ||
priv(self, 'lruList').unshiftNode(node) | ||
self[LRU_LIST].unshiftNode(node) | ||
} | ||
@@ -462,3 +457,3 @@ } | ||
function isStale (self, hit) { | ||
if (!hit || (!hit.maxAge && !priv(self, 'maxAge'))) { | ||
if (!hit || (!hit.maxAge && !self[MAX_AGE])) { | ||
return false | ||
@@ -471,3 +466,3 @@ } | ||
} else { | ||
stale = priv(self, 'maxAge') && (diff > priv(self, 'maxAge')) | ||
stale = self[MAX_AGE] && (diff > self[MAX_AGE]) | ||
} | ||
@@ -478,5 +473,5 @@ return stale | ||
function trim (self) { | ||
if (priv(self, 'length') > priv(self, 'max')) { | ||
for (var walker = priv(self, 'lruList').tail; | ||
priv(self, 'length') > priv(self, 'max') && walker !== null;) { | ||
if (self[LENGTH] > self[MAX]) { | ||
for (var walker = self[LRU_LIST].tail; | ||
self[LENGTH] > self[MAX] && walker !== null;) { | ||
// We know that we're about to delete this one, and also | ||
@@ -495,8 +490,8 @@ // what the next least recently used key will be, so just | ||
var hit = node.value | ||
if (priv(self, 'dispose')) { | ||
priv(self, 'dispose').call(this, hit.key, hit.value) | ||
if (self[DISPOSE]) { | ||
self[DISPOSE](hit.key, hit.value) | ||
} | ||
priv(self, 'length', priv(self, 'length') - hit.length) | ||
priv(self, 'cache').delete(hit.key) | ||
priv(self, 'lruList').removeNode(node) | ||
self[LENGTH] -= hit.length | ||
self[CACHE].delete(hit.key) | ||
self[LRU_LIST].removeNode(node) | ||
} | ||
@@ -503,0 +498,0 @@ } |
{ | ||
"name": "lru-cache", | ||
"description": "A cache object that deletes the least-recently-used items.", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"author": "Isaac Z. Schlueter <i@izs.me>", | ||
@@ -21,2 +21,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"standard": "^5.4.1", | ||
@@ -27,4 +28,4 @@ "tap": "^10.3.3" | ||
"dependencies": { | ||
"pseudomap": "^1.0.1", | ||
"yallist": "^2.0.0" | ||
"pseudomap": "^1.0.2", | ||
"yallist": "^2.1.2" | ||
}, | ||
@@ -31,0 +32,0 @@ "files": [ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
17340
3
409
Updatedpseudomap@^1.0.2
Updatedyallist@^2.1.2