wallet-core/node_modules/lru-cache/lib/lru-cache.js

470 lines
11 KiB
JavaScript
Raw Normal View History

2017-05-27 17:36:13 +02:00
module.exports = LRUCache
// This will be a proper iterable 'Map' in engines that support it,
// or a fakey-fake PseudoMap in older versions.
var Map = require('pseudomap')
var util = require('util')
// A linked list to keep track of recently-used-ness
var Yallist = require('yallist')
// use symbols if possible, otherwise just _props
var symbols = {}
var hasSymbol = typeof Symbol === 'function'
var makeSymbol
/* istanbul ignore if */
if (hasSymbol) {
makeSymbol = function (key) {
return Symbol.for(key)
}
2016-10-10 03:43:44 +02:00
} else {
2017-05-27 17:36:13 +02:00
makeSymbol = function (key) {
return '_' + key
}
2016-10-10 03:43:44 +02:00
}
2017-05-27 17:36:13 +02:00
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
}
2016-10-10 03:43:44 +02:00
}
function naiveLength () { return 1 }
2017-05-27 17:36:13 +02:00
// lruList is a yallist where the head is the youngest
// item, and the tail is the oldest. the list contains the Hit
// objects as the entries.
// Each Hit object has a reference to its Yallist.Node. This
// never changes.
//
// cache is a Map (or PseudoMap) that matches the keys to
// the Yallist.Node object.
2016-10-10 03:43:44 +02:00
function LRUCache (options) {
2017-05-27 17:36:13 +02:00
if (!(this instanceof LRUCache)) {
2016-10-10 03:43:44 +02:00
return new LRUCache(options)
2017-05-27 17:36:13 +02:00
}
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
if (typeof options === 'number') {
2016-10-10 03:43:44 +02:00
options = { max: options }
2017-05-27 17:36:13 +02:00
}
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
if (!options) {
2016-10-10 03:43:44 +02:00
options = {}
2017-05-27 17:36:13 +02:00
}
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
var max = priv(this, 'max', options.max)
2016-10-10 03:43:44 +02:00
// Kind of weird to have a default max of Infinity, but oh well.
2017-05-27 17:36:13 +02:00
if (!max ||
!(typeof max === 'number') ||
max <= 0) {
priv(this, 'max', Infinity)
}
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
var lc = options.length || naiveLength
if (typeof lc !== 'function') {
lc = naiveLength
}
priv(this, 'lengthCalculator', lc)
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
priv(this, 'allowStale', options.stale || false)
priv(this, 'maxAge', options.maxAge || 0)
priv(this, 'dispose', options.dispose)
2016-10-10 03:43:44 +02:00
this.reset()
}
// resize the cache when the max changes.
2017-05-27 17:36:13 +02:00
Object.defineProperty(LRUCache.prototype, 'max', {
set: function (mL) {
if (!mL || !(typeof mL === 'number') || mL <= 0) {
mL = Infinity
2016-10-10 03:43:44 +02:00
}
2017-05-27 17:36:13 +02:00
priv(this, 'max', mL)
trim(this)
},
get: function () {
return priv(this, 'max')
},
enumerable: true
})
Object.defineProperty(LRUCache.prototype, 'allowStale', {
set: function (allowStale) {
priv(this, 'allowStale', !!allowStale)
},
get: function () {
return priv(this, 'allowStale')
},
enumerable: true
})
Object.defineProperty(LRUCache.prototype, 'maxAge', {
set: function (mA) {
if (!mA || !(typeof mA === 'number') || mA < 0) {
mA = 0
}
priv(this, 'maxAge', mA)
trim(this)
},
get: function () {
return priv(this, 'maxAge')
},
enumerable: true
})
2016-10-10 03:43:44 +02:00
// resize the cache when the lengthCalculator changes.
2017-05-27 17:36:13 +02:00
Object.defineProperty(LRUCache.prototype, 'lengthCalculator', {
set: function (lC) {
if (typeof lC !== 'function') {
lC = naiveLength
2016-10-10 03:43:44 +02:00
}
2017-05-27 17:36:13 +02:00
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)
}, this)
}
trim(this)
},
get: function () { return priv(this, 'lengthCalculator') },
enumerable: true
})
Object.defineProperty(LRUCache.prototype, 'length', {
get: function () { return priv(this, 'length') },
enumerable: true
})
Object.defineProperty(LRUCache.prototype, 'itemCount', {
get: function () { return priv(this, 'lruList').length },
enumerable: true
})
LRUCache.prototype.rforEach = function (fn, thisp) {
thisp = thisp || this
for (var walker = priv(this, 'lruList').tail; walker !== null;) {
var prev = walker.prev
forEachStep(this, fn, walker, thisp)
walker = prev
}
}
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
function forEachStep (self, fn, node, thisp) {
var hit = node.value
if (isStale(self, hit)) {
del(self, node)
if (!priv(self, 'allowStale')) {
hit = undefined
}
}
if (hit) {
fn.call(thisp, hit.value, hit.key, self)
}
}
2016-10-10 03:43:44 +02:00
LRUCache.prototype.forEach = function (fn, thisp) {
thisp = thisp || this
2017-05-27 17:36:13 +02:00
for (var walker = priv(this, 'lruList').head; walker !== null;) {
var next = walker.next
forEachStep(this, fn, walker, thisp)
walker = next
2016-10-10 03:43:44 +02:00
}
}
LRUCache.prototype.keys = function () {
2017-05-27 17:36:13 +02:00
return priv(this, 'lruList').toArray().map(function (k) {
return k.key
}, this)
2016-10-10 03:43:44 +02:00
}
LRUCache.prototype.values = function () {
2017-05-27 17:36:13 +02:00
return priv(this, 'lruList').toArray().map(function (k) {
return k.value
}, this)
2016-10-10 03:43:44 +02:00
}
LRUCache.prototype.reset = function () {
2017-05-27 17:36:13 +02:00
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)
}, this)
2016-10-10 03:43:44 +02:00
}
2017-05-27 17:36:13 +02:00
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
2016-10-10 03:43:44 +02:00
}
LRUCache.prototype.dump = function () {
2017-05-27 17:36:13 +02:00
return priv(this, 'lruList').map(function (hit) {
2016-10-10 03:43:44 +02:00
if (!isStale(this, hit)) {
2017-05-27 17:36:13 +02:00
return {
2016-10-10 03:43:44 +02:00
k: hit.key,
v: hit.value,
e: hit.now + (hit.maxAge || 0)
2017-05-27 17:36:13 +02:00
}
2016-10-10 03:43:44 +02:00
}
2017-05-27 17:36:13 +02:00
}, this).toArray().filter(function (h) {
return h
})
2016-10-10 03:43:44 +02:00
}
LRUCache.prototype.dumpLru = function () {
2017-05-27 17:36:13 +02:00
return priv(this, 'lruList')
}
LRUCache.prototype.inspect = function (n, opts) {
var str = 'LRUCache {'
var extras = false
var as = priv(this, 'allowStale')
if (as) {
str += '\n allowStale: true'
extras = true
}
var max = priv(this, 'max')
if (max && max !== Infinity) {
if (extras) {
str += ','
}
str += '\n max: ' + util.inspect(max, opts)
extras = true
}
var maxAge = priv(this, 'maxAge')
if (maxAge) {
if (extras) {
str += ','
}
str += '\n maxAge: ' + util.inspect(maxAge, opts)
extras = true
}
var lc = priv(this, 'lengthCalculator')
if (lc && lc !== naiveLength) {
if (extras) {
str += ','
}
str += '\n length: ' + util.inspect(priv(this, 'length'), opts)
extras = true
}
var didFirst = false
priv(this, 'lruList').forEach(function (item) {
if (didFirst) {
str += ',\n '
} else {
if (extras) {
str += ',\n'
}
didFirst = true
str += '\n '
}
var key = util.inspect(item.key).split('\n').join('\n ')
var val = { value: item.value }
if (item.maxAge !== maxAge) {
val.maxAge = item.maxAge
}
if (lc !== naiveLength) {
val.length = item.length
}
if (isStale(this, item)) {
val.stale = true
}
val = util.inspect(val, opts).split('\n').join('\n ')
str += key + ' => ' + val
})
if (didFirst || extras) {
str += '\n'
}
str += '}'
return str
2016-10-10 03:43:44 +02:00
}
LRUCache.prototype.set = function (key, value, maxAge) {
2017-05-27 17:36:13 +02:00
maxAge = maxAge || priv(this, 'maxAge')
2016-10-10 03:43:44 +02:00
var now = maxAge ? Date.now() : 0
2017-05-27 17:36:13 +02:00
var len = priv(this, 'lengthCalculator').call(this, value, key)
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
if (priv(this, 'cache').has(key)) {
if (len > priv(this, 'max')) {
del(this, priv(this, 'cache').get(key))
2016-10-10 03:43:44 +02:00
return false
}
2017-05-27 17:36:13 +02:00
var node = priv(this, 'cache').get(key)
var item = node.value
// dispose of the old one before overwriting
if (priv(this, 'dispose')) {
priv(this, 'dispose').call(this, key, item.value)
}
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
item.now = now
item.maxAge = maxAge
item.value = value
priv(this, 'length', priv(this, 'length') + (len - item.length))
item.length = len
this.get(key)
trim(this)
2016-10-10 03:43:44 +02:00
return true
}
2017-05-27 17:36:13 +02:00
var hit = new Entry(key, value, len, now, maxAge)
2016-10-10 03:43:44 +02:00
// oversized objects fall out of cache automatically.
2017-05-27 17:36:13 +02:00
if (hit.length > priv(this, 'max')) {
if (priv(this, 'dispose')) {
priv(this, 'dispose').call(this, key, value)
}
2016-10-10 03:43:44 +02:00
return false
}
2017-05-27 17:36:13 +02:00
priv(this, 'length', priv(this, 'length') + hit.length)
priv(this, 'lruList').unshift(hit)
priv(this, 'cache').set(key, priv(this, 'lruList').head)
trim(this)
2016-10-10 03:43:44 +02:00
return true
}
LRUCache.prototype.has = function (key) {
2017-05-27 17:36:13 +02:00
if (!priv(this, 'cache').has(key)) return false
var hit = priv(this, 'cache').get(key).value
2016-10-10 03:43:44 +02:00
if (isStale(this, hit)) {
return false
}
return true
}
LRUCache.prototype.get = function (key) {
return get(this, key, true)
}
LRUCache.prototype.peek = function (key) {
return get(this, key, false)
}
LRUCache.prototype.pop = function () {
2017-05-27 17:36:13 +02:00
var node = priv(this, 'lruList').tail
if (!node) return null
del(this, node)
return node.value
2016-10-10 03:43:44 +02:00
}
LRUCache.prototype.del = function (key) {
2017-05-27 17:36:13 +02:00
del(this, priv(this, 'cache').get(key))
2016-10-10 03:43:44 +02:00
}
LRUCache.prototype.load = function (arr) {
2017-05-27 17:36:13 +02:00
// reset the cache
this.reset()
2016-10-10 03:43:44 +02:00
var now = Date.now()
2017-05-27 17:36:13 +02:00
// A previous serialized cache has the most recent items first
for (var l = arr.length - 1; l >= 0; l--) {
2016-10-10 03:43:44 +02:00
var hit = arr[l]
var expiresAt = hit.e || 0
if (expiresAt === 0) {
2017-05-27 17:36:13 +02:00
// the item was created without expiration in a non aged cache
2016-10-10 03:43:44 +02:00
this.set(hit.k, hit.v)
} else {
var maxAge = expiresAt - now
2017-05-27 17:36:13 +02:00
// dont add already expired items
if (maxAge > 0) {
this.set(hit.k, hit.v, maxAge)
}
2016-10-10 03:43:44 +02:00
}
}
}
2017-05-27 17:36:13 +02:00
LRUCache.prototype.prune = function () {
var self = this
priv(this, 'cache').forEach(function (value, key) {
get(self, key, false)
})
}
2016-10-10 03:43:44 +02:00
function get (self, key, doUse) {
2017-05-27 17:36:13 +02:00
var node = priv(self, 'cache').get(key)
if (node) {
var hit = node.value
2016-10-10 03:43:44 +02:00
if (isStale(self, hit)) {
2017-05-27 17:36:13 +02:00
del(self, node)
if (!priv(self, 'allowStale')) hit = undefined
2016-10-10 03:43:44 +02:00
} else {
2017-05-27 17:36:13 +02:00
if (doUse) {
priv(self, 'lruList').unshiftNode(node)
}
2016-10-10 03:43:44 +02:00
}
if (hit) hit = hit.value
}
return hit
}
2017-05-27 17:36:13 +02:00
function isStale (self, hit) {
if (!hit || (!hit.maxAge && !priv(self, 'maxAge'))) {
return false
}
var stale = false
2016-10-10 03:43:44 +02:00
var diff = Date.now() - hit.now
if (hit.maxAge) {
stale = diff > hit.maxAge
} else {
2017-05-27 17:36:13 +02:00
stale = priv(self, 'maxAge') && (diff > priv(self, 'maxAge'))
2016-10-10 03:43:44 +02:00
}
2017-05-27 17:36:13 +02:00
return stale
2016-10-10 03:43:44 +02:00
}
function trim (self) {
2017-05-27 17:36:13 +02:00
if (priv(self, 'length') > priv(self, 'max')) {
for (var walker = priv(self, 'lruList').tail;
priv(self, 'length') > priv(self, 'max') && walker !== null;) {
// We know that we're about to delete this one, and also
// what the next least recently used key will be, so just
// go ahead and set it now.
var prev = walker.prev
del(self, walker)
walker = prev
}
}
2016-10-10 03:43:44 +02:00
}
2017-05-27 17:36:13 +02:00
function del (self, node) {
if (node) {
var hit = node.value
if (priv(self, 'dispose')) {
priv(self, 'dispose').call(this, hit.key, hit.value)
}
priv(self, 'length', priv(self, 'length') - hit.length)
priv(self, 'cache').delete(hit.key)
priv(self, 'lruList').removeNode(node)
2016-10-10 03:43:44 +02:00
}
}
// classy, since V8 prefers predictable objects.
2017-05-27 17:36:13 +02:00
function Entry (key, value, length, now, maxAge) {
2016-10-10 03:43:44 +02:00
this.key = key
this.value = value
this.length = length
this.now = now
2017-05-27 17:36:13 +02:00
this.maxAge = maxAge || 0
2016-10-10 03:43:44 +02:00
}