| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict' |
| 2 | |
| 3 | module.exports = LRUCache |
| 4 | |
| 5 | // This will be a proper iterable 'Map' in engines that support it, |
| 6 | // or a fakey-fake PseudoMap in older versions. |
| 7 | var Map = require('pseudomap') |
| 8 | var util = require('util') |
| 9 | |
| 10 | // A linked list to keep track of recently-used-ness |
| 11 | var Yallist = require('yallist') |
| 12 | |
| 13 | // use symbols if possible, otherwise just _props |
| 14 | var hasSymbol = typeof Symbol === 'function' && process.env._nodeLRUCacheForceNoSymbol !== '1' |
| 15 | var makeSymbol |
| 16 | if (hasSymbol) { |
| 17 | makeSymbol = function (key) { |
| 18 | return Symbol(key) |
| 19 | } |
| 20 | } else { |
| 21 | makeSymbol = function (key) { |
| 22 | return '_' + key |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | var MAX = makeSymbol('max') |
| 27 | var LENGTH = makeSymbol('length') |
| 28 | var LENGTH_CALCULATOR = makeSymbol('lengthCalculator') |
| 29 | var ALLOW_STALE = makeSymbol('allowStale') |
| 30 | var MAX_AGE = makeSymbol('maxAge') |
| 31 | var DISPOSE = makeSymbol('dispose') |
| 32 | var NO_DISPOSE_ON_SET = makeSymbol('noDisposeOnSet') |
| 33 | var LRU_LIST = makeSymbol('lruList') |
| 34 | var CACHE = makeSymbol('cache') |
| 35 | |
| 36 | function naiveLength () { return 1 } |
| 37 | |
| 38 | // lruList is a yallist where the head is the youngest |
| 39 | // item, and the tail is the oldest. the list contains the Hit |
| 40 | // objects as the entries. |
| 41 | // Each Hit object has a reference to its Yallist.Node. This |
| 42 | // never changes. |
| 43 | // |
| 44 | // cache is a Map (or PseudoMap) that matches the keys to |
| 45 | // the Yallist.Node object. |
| 46 | function LRUCache (options) { |
| 47 | if (!(this instanceof LRUCache)) { |
| 48 | return new LRUCache(options) |
| 49 | } |
| 50 | |
| 51 | if (typeof options === 'number') { |
| 52 | options = { max: options } |
| 53 | } |
| 54 | |
| 55 | if (!options) { |
| 56 | options = {} |
| 57 | } |
| 58 | |
| 59 | var max = this[MAX] = options.max |
| 60 | // Kind of weird to have a default max of Infinity, but oh well. |
| 61 | if (!max || |
| 62 | !(typeof max === 'number') || |
| 63 | max <= 0) { |
| 64 | this[MAX] = Infinity |
| 65 | } |
| 66 | |
| 67 | var lc = options.length || naiveLength |
| 68 | if (typeof lc !== 'function') { |
| 69 | lc = naiveLength |
| 70 | } |
| 71 | this[LENGTH_CALCULATOR] = lc |
| 72 | |
| 73 | this[ALLOW_STALE] = options.stale || false |
| 74 | this[MAX_AGE] = options.maxAge || 0 |
| 75 | this[DISPOSE] = options.dispose |
| 76 | this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false |
| 77 | this.reset() |
| 78 | } |
| 79 | |
| 80 | // resize the cache when the max changes. |
| 81 | Object.defineProperty(LRUCache.prototype, 'max', { |
| 82 | set: function (mL) { |
| 83 | if (!mL || !(typeof mL === 'number') || mL <= 0) { |
| 84 | mL = Infinity |
| 85 | } |
| 86 | this[MAX] = mL |
| 87 | trim(this) |
| 88 | }, |
| 89 | get: function () { |
| 90 | return this[MAX] |
| 91 | }, |
| 92 | enumerable: true |
| 93 | }) |
| 94 | |
| 95 | Object.defineProperty(LRUCache.prototype, 'allowStale', { |
| 96 | set: function (allowStale) { |
| 97 | this[ALLOW_STALE] = !!allowStale |
| 98 | }, |
| 99 | get: function () { |
| 100 | return this[ALLOW_STALE] |
| 101 | }, |
| 102 | enumerable: true |
| 103 | }) |
| 104 | |
| 105 | Object.defineProperty(LRUCache.prototype, 'maxAge', { |
| 106 | set: function (mA) { |
| 107 | if (!mA || !(typeof mA === 'number') || mA < 0) { |
| 108 | mA = 0 |
| 109 | } |
| 110 | this[MAX_AGE] = mA |
| 111 | trim(this) |
| 112 | }, |
| 113 | get: function () { |
| 114 | return this[MAX_AGE] |
| 115 | }, |
| 116 | enumerable: true |
| 117 | }) |
| 118 | |
| 119 | // resize the cache when the lengthCalculator changes. |
| 120 | Object.defineProperty(LRUCache.prototype, 'lengthCalculator', { |
| 121 | set: function (lC) { |
| 122 | if (typeof lC !== 'function') { |
| 123 | lC = naiveLength |
| 124 | } |
| 125 | if (lC !== this[LENGTH_CALCULATOR]) { |
| 126 | this[LENGTH_CALCULATOR] = lC |
| 127 | this[LENGTH] = 0 |
| 128 | this[LRU_LIST].forEach(function (hit) { |
| 129 | hit.length = this[LENGTH_CALCULATOR](hit.value, hit.key) |
| 130 | this[LENGTH] += hit.length |
| 131 | }, this) |
| 132 | } |
| 133 | trim(this) |
| 134 | }, |
| 135 | get: function () { return this[LENGTH_CALCULATOR] }, |
| 136 | enumerable: true |
| 137 | }) |
| 138 | |
| 139 | Object.defineProperty(LRUCache.prototype, 'length', { |
| 140 | get: function () { return this[LENGTH] }, |
| 141 | enumerable: true |
| 142 | }) |
| 143 | |
| 144 | Object.defineProperty(LRUCache.prototype, 'itemCount', { |
| 145 | get: function () { return this[LRU_LIST].length }, |
| 146 | enumerable: true |
| 147 | }) |
| 148 | |
| 149 | LRUCache.prototype.rforEach = function (fn, thisp) { |
| 150 | thisp = thisp || this |
| 151 | for (var walker = this[LRU_LIST].tail; walker !== null;) { |
| 152 | var prev = walker.prev |
| 153 | forEachStep(this, fn, walker, thisp) |
| 154 | walker = prev |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | function forEachStep (self, fn, node, thisp) { |
| 159 | var hit = node.value |
| 160 | if (isStale(self, hit)) { |
| 161 | del(self, node) |
| 162 | if (!self[ALLOW_STALE]) { |
| 163 | hit = undefined |
| 164 | } |
| 165 | } |
| 166 | if (hit) { |
| 167 | fn.call(thisp, hit.value, hit.key, self) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | LRUCache.prototype.forEach = function (fn, thisp) { |
| 172 | thisp = thisp || this |
| 173 | for (var walker = this[LRU_LIST].head; walker !== null;) { |
| 174 | var next = walker.next |
| 175 | forEachStep(this, fn, walker, thisp) |
| 176 | walker = next |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | LRUCache.prototype.keys = function () { |
| 181 | return this[LRU_LIST].toArray().map(function (k) { |
| 182 | return k.key |
| 183 | }, this) |
| 184 | } |
| 185 | |
| 186 | LRUCache.prototype.values = function () { |
| 187 | return this[LRU_LIST].toArray().map(function (k) { |
| 188 | return k.value |
| 189 | }, this) |
| 190 | } |
| 191 | |
| 192 | LRUCache.prototype.reset = function () { |
| 193 | if (this[DISPOSE] && |
| 194 | this[LRU_LIST] && |
| 195 | this[LRU_LIST].length) { |
| 196 | this[LRU_LIST].forEach(function (hit) { |
| 197 | this[DISPOSE](hit.key, hit.value) |
| 198 | }, this) |
| 199 | } |
| 200 | |
| 201 | this[CACHE] = new Map() // hash of items by key |
| 202 | this[LRU_LIST] = new Yallist() // list of items in order of use recency |
| 203 | this[LENGTH] = 0 // length of items in the list |
| 204 | } |
| 205 | |
| 206 | LRUCache.prototype.dump = function () { |
| 207 | return this[LRU_LIST].map(function (hit) { |
| 208 | if (!isStale(this, hit)) { |
| 209 | return { |
| 210 | k: hit.key, |
| 211 | v: hit.value, |
| 212 | e: hit.now + (hit.maxAge || 0) |
| 213 | } |
| 214 | } |
| 215 | }, this).toArray().filter(function (h) { |
| 216 | return h |
| 217 | }) |
| 218 | } |
| 219 | |
| 220 | LRUCache.prototype.dumpLru = function () { |
| 221 | return this[LRU_LIST] |
| 222 | } |
| 223 | |
| 224 | /* istanbul ignore next */ |
| 225 | LRUCache.prototype.inspect = function (n, opts) { |
| 226 | var str = 'LRUCache {' |
| 227 | var extras = false |
| 228 | |
| 229 | var as = this[ALLOW_STALE] |
| 230 | if (as) { |
| 231 | str += '\n allowStale: true' |
| 232 | extras = true |
| 233 | } |
| 234 | |
| 235 | var max = this[MAX] |
| 236 | if (max && max !== Infinity) { |
| 237 | if (extras) { |
| 238 | str += ',' |
| 239 | } |
| 240 | str += '\n max: ' + util.inspect(max, opts) |
| 241 | extras = true |
| 242 | } |
| 243 | |
| 244 | var maxAge = this[MAX_AGE] |
| 245 | if (maxAge) { |
| 246 | if (extras) { |
| 247 | str += ',' |
| 248 | } |
| 249 | str += '\n maxAge: ' + util.inspect(maxAge, opts) |
| 250 | extras = true |
| 251 | } |
| 252 | |
| 253 | var lc = this[LENGTH_CALCULATOR] |
| 254 | if (lc && lc !== naiveLength) { |
| 255 | if (extras) { |
| 256 | str += ',' |
| 257 | } |
| 258 | str += '\n length: ' + util.inspect(this[LENGTH], opts) |
| 259 | extras = true |
| 260 | } |
| 261 | |
| 262 | var didFirst = false |
| 263 | this[LRU_LIST].forEach(function (item) { |
| 264 | if (didFirst) { |
| 265 | str += ',\n ' |
| 266 | } else { |
| 267 | if (extras) { |
| 268 | str += ',\n' |
| 269 | } |
| 270 | didFirst = true |
| 271 | str += '\n ' |
| 272 | } |
| 273 | var key = util.inspect(item.key).split('\n').join('\n ') |
| 274 | var val = { value: item.value } |
| 275 | if (item.maxAge !== maxAge) { |
| 276 | val.maxAge = item.maxAge |
| 277 | } |
| 278 | if (lc !== naiveLength) { |
| 279 | val.length = item.length |
| 280 | } |
| 281 | if (isStale(this, item)) { |
| 282 | val.stale = true |
| 283 | } |
| 284 | |
| 285 | val = util.inspect(val, opts).split('\n').join('\n ') |
| 286 | str += key + ' => ' + val |
| 287 | }) |
| 288 | |
| 289 | if (didFirst || extras) { |
| 290 | str += '\n' |
| 291 | } |
| 292 | str += '}' |
| 293 | |
| 294 | return str |
| 295 | } |
| 296 | |
| 297 | LRUCache.prototype.set = function (key, value, maxAge) { |
| 298 | maxAge = maxAge || this[MAX_AGE] |
| 299 | |
| 300 | var now = maxAge ? Date.now() : 0 |
| 301 | var len = this[LENGTH_CALCULATOR](value, key) |
| 302 | |
| 303 | if (this[CACHE].has(key)) { |
| 304 | if (len > this[MAX]) { |
| 305 | del(this, this[CACHE].get(key)) |
| 306 | return false |
| 307 | } |
| 308 | |
| 309 | var node = this[CACHE].get(key) |
| 310 | var item = node.value |
| 311 | |
| 312 | // dispose of the old one before overwriting |
| 313 | // split out into 2 ifs for better coverage tracking |
| 314 | if (this[DISPOSE]) { |
| 315 | if (!this[NO_DISPOSE_ON_SET]) { |
| 316 | this[DISPOSE](key, item.value) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | item.now = now |
| 321 | item.maxAge = maxAge |
| 322 | item.value = value |
| 323 | this[LENGTH] += len - item.length |
| 324 | item.length = len |
| 325 | this.get(key) |
| 326 | trim(this) |
| 327 | return true |
| 328 | } |
| 329 | |
| 330 | var hit = new Entry(key, value, len, now, maxAge) |
| 331 | |
| 332 | // oversized objects fall out of cache automatically. |
| 333 | if (hit.length > this[MAX]) { |
| 334 | if (this[DISPOSE]) { |
| 335 | this[DISPOSE](key, value) |
| 336 | } |
| 337 | return false |
| 338 | } |
| 339 | |
| 340 | this[LENGTH] += hit.length |
| 341 | this[LRU_LIST].unshift(hit) |
| 342 | this[CACHE].set(key, this[LRU_LIST].head) |
| 343 | trim(this) |
| 344 | return true |
| 345 | } |
| 346 | |
| 347 | LRUCache.prototype.has = function (key) { |
| 348 | if (!this[CACHE].has(key)) return false |
| 349 | var hit = this[CACHE].get(key).value |
| 350 | if (isStale(this, hit)) { |
| 351 | return false |
| 352 | } |
| 353 | return true |
| 354 | } |
| 355 | |
| 356 | LRUCache.prototype.get = function (key) { |
| 357 | return get(this, key, true) |
| 358 | } |
| 359 | |
| 360 | LRUCache.prototype.peek = function (key) { |
| 361 | return get(this, key, false) |
| 362 | } |
| 363 | |
| 364 | LRUCache.prototype.pop = function () { |
| 365 | var node = this[LRU_LIST].tail |
| 366 | if (!node) return null |
| 367 | del(this, node) |
| 368 | return node.value |
| 369 | } |
| 370 | |
| 371 | LRUCache.prototype.del = function (key) { |
| 372 | del(this, this[CACHE].get(key)) |
| 373 | } |
| 374 | |
| 375 | LRUCache.prototype.load = function (arr) { |
| 376 | // reset the cache |
| 377 | this.reset() |
| 378 | |
| 379 | var now = Date.now() |
| 380 | // A previous serialized cache has the most recent items first |
| 381 | for (var l = arr.length - 1; l >= 0; l--) { |
| 382 | var hit = arr[l] |
| 383 | var expiresAt = hit.e || 0 |
| 384 | if (expiresAt === 0) { |
| 385 | // the item was created without expiration in a non aged cache |
| 386 | this.set(hit.k, hit.v) |
| 387 | } else { |
| 388 | var maxAge = expiresAt - now |
| 389 | // dont add already expired items |
| 390 | if (maxAge > 0) { |
| 391 | this.set(hit.k, hit.v, maxAge) |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | LRUCache.prototype.prune = function () { |
| 398 | var self = this |
| 399 | this[CACHE].forEach(function (value, key) { |
| 400 | get(self, key, false) |
| 401 | }) |
| 402 | } |
| 403 | |
| 404 | function get (self, key, doUse) { |
| 405 | var node = self[CACHE].get(key) |
| 406 | if (node) { |
| 407 | var hit = node.value |
| 408 | if (isStale(self, hit)) { |
| 409 | del(self, node) |
| 410 | if (!self[ALLOW_STALE]) hit = undefined |
| 411 | } else { |
| 412 | if (doUse) { |
| 413 | self[LRU_LIST].unshiftNode(node) |
| 414 | } |
| 415 | } |
| 416 | if (hit) hit = hit.value |
| 417 | } |
| 418 | return hit |
| 419 | } |
| 420 | |
| 421 | function isStale (self, hit) { |
| 422 | if (!hit || (!hit.maxAge && !self[MAX_AGE])) { |
| 423 | return false |
| 424 | } |
| 425 | var stale = false |
| 426 | var diff = Date.now() - hit.now |
| 427 | if (hit.maxAge) { |
| 428 | stale = diff > hit.maxAge |
| 429 | } else { |
| 430 | stale = self[MAX_AGE] && (diff > self[MAX_AGE]) |
| 431 | } |
| 432 | return stale |
| 433 | } |
| 434 | |
| 435 | function trim (self) { |
| 436 | if (self[LENGTH] > self[MAX]) { |
| 437 | for (var walker = self[LRU_LIST].tail; |
| 438 | self[LENGTH] > self[MAX] && walker !== null;) { |
| 439 | // We know that we're about to delete this one, and also |
| 440 | // what the next least recently used key will be, so just |
| 441 | // go ahead and set it now. |
| 442 | var prev = walker.prev |
| 443 | del(self, walker) |
| 444 | walker = prev |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | function del (self, node) { |
| 450 | if (node) { |
| 451 | var hit = node.value |
| 452 | if (self[DISPOSE]) { |
| 453 | self[DISPOSE](hit.key, hit.value) |
| 454 | } |
| 455 | self[LENGTH] -= hit.length |
| 456 | self[CACHE].delete(hit.key) |
| 457 | self[LRU_LIST].removeNode(node) |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // classy, since V8 prefers predictable objects. |
| 462 | function Entry (key, value, length, now, maxAge) { |
| 463 | this.key = key |
| 464 | this.value = value |
| 465 | this.length = length |
| 466 | this.now = now |
| 467 | this.maxAge = maxAge || 0 |
| 468 | } |