odin-javascript-exercises/node_modules/lru-cache/index.js

335 lines
8 KiB
JavaScript
Raw Normal View History

2017-12-15 10:56:14 -08:00
'use strict'
// A linked list to keep track of recently-used-ness
const Yallist = require('yallist')
2017-12-15 10:56:14 -08:00
const MAX = Symbol('max')
const LENGTH = Symbol('length')
const LENGTH_CALCULATOR = Symbol('lengthCalculator')
const ALLOW_STALE = Symbol('allowStale')
const MAX_AGE = Symbol('maxAge')
const DISPOSE = Symbol('dispose')
const NO_DISPOSE_ON_SET = Symbol('noDisposeOnSet')
const LRU_LIST = Symbol('lruList')
const CACHE = Symbol('cache')
const UPDATE_AGE_ON_GET = Symbol('updateAgeOnGet')
2017-12-15 10:56:14 -08:00
const naiveLength = () => 1
2017-12-15 10:56:14 -08: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.
class LRUCache {
constructor (options) {
if (typeof options === 'number')
options = { max: options }
if (!options)
options = {}
if (options.max && (typeof options.max !== 'number' || options.max < 0))
throw new TypeError('max must be a non-negative number')
// Kind of weird to have a default max of Infinity, but oh well.
const max = this[MAX] = options.max || Infinity
const lc = options.length || naiveLength
this[LENGTH_CALCULATOR] = (typeof lc !== 'function') ? naiveLength : lc
this[ALLOW_STALE] = options.stale || false
if (options.maxAge && typeof options.maxAge !== 'number')
throw new TypeError('maxAge must be a number')
this[MAX_AGE] = options.maxAge || 0
this[DISPOSE] = options.dispose
this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false
this[UPDATE_AGE_ON_GET] = options.updateAgeOnGet || false
this.reset()
2017-12-15 10:56:14 -08:00
}
// resize the cache when the max changes.
set max (mL) {
if (typeof mL !== 'number' || mL < 0)
throw new TypeError('max must be a non-negative number')
2017-12-15 10:56:14 -08:00
this[MAX] = mL || Infinity
trim(this)
2017-12-15 10:56:14 -08:00
}
get max () {
return this[MAX]
2017-12-15 10:56:14 -08:00
}
set allowStale (allowStale) {
this[ALLOW_STALE] = !!allowStale
}
get allowStale () {
return this[ALLOW_STALE]
2017-12-15 10:56:14 -08:00
}
set maxAge (mA) {
if (typeof mA !== 'number')
throw new TypeError('maxAge must be a non-negative number')
2017-12-15 10:56:14 -08:00
this[MAX_AGE] = mA
trim(this)
}
get maxAge () {
2017-12-15 10:56:14 -08:00
return this[MAX_AGE]
}
// resize the cache when the lengthCalculator changes.
set lengthCalculator (lC) {
if (typeof lC !== 'function')
2017-12-15 10:56:14 -08:00
lC = naiveLength
2017-12-15 10:56:14 -08:00
if (lC !== this[LENGTH_CALCULATOR]) {
this[LENGTH_CALCULATOR] = lC
this[LENGTH] = 0
this[LRU_LIST].forEach(hit => {
2017-12-15 10:56:14 -08:00
hit.length = this[LENGTH_CALCULATOR](hit.value, hit.key)
this[LENGTH] += hit.length
})
2017-12-15 10:56:14 -08:00
}
trim(this)
}
get lengthCalculator () { return this[LENGTH_CALCULATOR] }
2017-12-15 10:56:14 -08:00
get length () { return this[LENGTH] }
get itemCount () { return this[LRU_LIST].length }
rforEach (fn, thisp) {
thisp = thisp || this
for (let walker = this[LRU_LIST].tail; walker !== null;) {
const prev = walker.prev
forEachStep(this, fn, walker, thisp)
walker = prev
2017-12-15 10:56:14 -08:00
}
}
forEach (fn, thisp) {
thisp = thisp || this
for (let walker = this[LRU_LIST].head; walker !== null;) {
const next = walker.next
forEachStep(this, fn, walker, thisp)
walker = next
}
2017-12-15 10:56:14 -08:00
}
keys () {
return this[LRU_LIST].toArray().map(k => k.key)
2017-12-15 10:56:14 -08:00
}
values () {
return this[LRU_LIST].toArray().map(k => k.value)
}
2017-12-15 10:56:14 -08:00
reset () {
if (this[DISPOSE] &&
this[LRU_LIST] &&
this[LRU_LIST].length) {
this[LRU_LIST].forEach(hit => this[DISPOSE](hit.key, hit.value))
}
2017-12-15 10:56:14 -08:00
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
2017-12-15 10:56:14 -08:00
}
dump () {
return this[LRU_LIST].map(hit =>
isStale(this, hit) ? false : {
2017-12-15 10:56:14 -08:00
k: hit.key,
v: hit.value,
e: hit.now + (hit.maxAge || 0)
}).toArray().filter(h => h)
2017-12-15 10:56:14 -08:00
}
dumpLru () {
return this[LRU_LIST]
2017-12-15 10:56:14 -08:00
}
set (key, value, maxAge) {
maxAge = maxAge || this[MAX_AGE]
2017-12-15 10:56:14 -08:00
if (maxAge && typeof maxAge !== 'number')
throw new TypeError('maxAge must be a number')
2017-12-15 10:56:14 -08:00
const now = maxAge ? Date.now() : 0
const len = this[LENGTH_CALCULATOR](value, key)
if (this[CACHE].has(key)) {
if (len > this[MAX]) {
del(this, this[CACHE].get(key))
return false
2017-12-15 10:56:14 -08:00
}
const node = this[CACHE].get(key)
const item = node.value
2017-12-15 10:56:14 -08:00
// dispose of the old one before overwriting
// split out into 2 ifs for better coverage tracking
if (this[DISPOSE]) {
if (!this[NO_DISPOSE_ON_SET])
this[DISPOSE](key, item.value)
}
2017-12-15 10:56:14 -08:00
item.now = now
item.maxAge = maxAge
item.value = value
this[LENGTH] += len - item.length
item.length = len
this.get(key)
trim(this)
return true
}
2017-12-15 10:56:14 -08:00
const hit = new Entry(key, value, len, now, maxAge)
2017-12-15 10:56:14 -08:00
// oversized objects fall out of cache automatically.
if (hit.length > this[MAX]) {
if (this[DISPOSE])
this[DISPOSE](key, value)
2017-12-15 10:56:14 -08:00
return false
}
this[LENGTH] += hit.length
this[LRU_LIST].unshift(hit)
this[CACHE].set(key, this[LRU_LIST].head)
2017-12-15 10:56:14 -08:00
trim(this)
return true
}
has (key) {
if (!this[CACHE].has(key)) return false
const hit = this[CACHE].get(key).value
return !isStale(this, hit)
2017-12-15 10:56:14 -08:00
}
get (key) {
return get(this, key, true)
2017-12-15 10:56:14 -08:00
}
peek (key) {
return get(this, key, false)
}
2017-12-15 10:56:14 -08:00
pop () {
const node = this[LRU_LIST].tail
if (!node)
return null
2017-12-15 10:56:14 -08:00
del(this, node)
return node.value
}
2017-12-15 10:56:14 -08:00
del (key) {
del(this, this[CACHE].get(key))
}
2017-12-15 10:56:14 -08:00
load (arr) {
// reset the cache
this.reset()
const now = Date.now()
// A previous serialized cache has the most recent items first
for (let l = arr.length - 1; l >= 0; l--) {
const hit = arr[l]
const 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 {
const maxAge = expiresAt - now
// dont add already expired items
if (maxAge > 0) {
this.set(hit.k, hit.v, maxAge)
}
2017-12-15 10:56:14 -08:00
}
}
}
prune () {
this[CACHE].forEach((value, key) => get(this, key, false))
}
2017-12-15 10:56:14 -08:00
}
const get = (self, key, doUse) => {
const node = self[CACHE].get(key)
2017-12-15 10:56:14 -08:00
if (node) {
const hit = node.value
2017-12-15 10:56:14 -08:00
if (isStale(self, hit)) {
del(self, node)
if (!self[ALLOW_STALE])
return undefined
2017-12-15 10:56:14 -08:00
} else {
if (doUse) {
if (self[UPDATE_AGE_ON_GET])
node.value.now = Date.now()
2017-12-15 10:56:14 -08:00
self[LRU_LIST].unshiftNode(node)
}
}
return hit.value
2017-12-15 10:56:14 -08:00
}
}
const isStale = (self, hit) => {
if (!hit || (!hit.maxAge && !self[MAX_AGE]))
2017-12-15 10:56:14 -08:00
return false
const diff = Date.now() - hit.now
return hit.maxAge ? diff > hit.maxAge
: self[MAX_AGE] && (diff > self[MAX_AGE])
2017-12-15 10:56:14 -08:00
}
const trim = self => {
2017-12-15 10:56:14 -08:00
if (self[LENGTH] > self[MAX]) {
for (let walker = self[LRU_LIST].tail;
self[LENGTH] > self[MAX] && walker !== null;) {
2017-12-15 10:56:14 -08:00
// 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.
const prev = walker.prev
2017-12-15 10:56:14 -08:00
del(self, walker)
walker = prev
}
}
}
const del = (self, node) => {
2017-12-15 10:56:14 -08:00
if (node) {
const hit = node.value
if (self[DISPOSE])
2017-12-15 10:56:14 -08:00
self[DISPOSE](hit.key, hit.value)
2017-12-15 10:56:14 -08:00
self[LENGTH] -= hit.length
self[CACHE].delete(hit.key)
self[LRU_LIST].removeNode(node)
}
}
class Entry {
constructor (key, value, length, now, maxAge) {
this.key = key
this.value = value
this.length = length
this.now = now
this.maxAge = maxAge || 0
}
}
const forEachStep = (self, fn, node, thisp) => {
let hit = node.value
if (isStale(self, hit)) {
del(self, node)
if (!self[ALLOW_STALE])
hit = undefined
}
if (hit)
fn.call(thisp, hit.value, hit.key, self)
2017-12-15 10:56:14 -08:00
}
module.exports = LRUCache