UNPKG

quick-lru-ts

Version:

Simple “Least Recently Used” (LRU) cache

556 lines (548 loc) 22.1 kB
"use strict"; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuickLRU = void 0; var QuickLRU = /** @class */ (function () { /** Simple ["Least Recently Used" (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29). The instance is an [`Iterable`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols) of `[key, value]` pairs so you can use it directly in a [`for…of`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of) loop. @example ``` import { QuickLRU } from 'quick-lru-ts'; const lru = new QuickLRU({maxSize: 1000}); lru.set('🦄', '🌈'); lru.has('🦄'); //=> true lru.get('🦄'); //=> '🌈' ``` */ function QuickLRU(options) { if (!(options.maxSize && options.maxSize > 0)) { throw new TypeError("`maxSize` must be a number greater than 0"); } if (typeof options.maxAge === "number" && options.maxAge === 0) { throw new TypeError("`maxAge` must be a number greater than 0"); } // TODO: Use private class fields when ESLint supports them. this.maxSize = options.maxSize; this.maxAge = options.maxAge || Number.POSITIVE_INFINITY; this.onEviction = options.onEviction; this.cache = new Map(); this.oldCache = new Map(); this._size = 0; } // TODO: Use private class methods when targeting Node.js 16. QuickLRU.prototype._emitEvictions = function (cache) { var e_1, _a; if (typeof this.onEviction !== "function") { return; } try { for (var cache_1 = __values(cache), cache_1_1 = cache_1.next(); !cache_1_1.done; cache_1_1 = cache_1.next()) { var _b = __read(cache_1_1.value, 2), key = _b[0], item = _b[1]; this.onEviction(key, item.value); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (cache_1_1 && !cache_1_1.done && (_a = cache_1.return)) _a.call(cache_1); } finally { if (e_1) throw e_1.error; } } }; QuickLRU.prototype._deleteIfExpired = function (key, item) { if (item === undefined) return true; if (typeof item.expiry === "number" && item.expiry <= Date.now()) { if (typeof this.onEviction === "function") { this.onEviction(key, item.value); } return this.delete(key); } return false; }; QuickLRU.prototype._getOrDeleteIfExpired = function (key, item) { var deleted = this._deleteIfExpired(key, item); if (deleted === false) { return item.value; } }; QuickLRU.prototype._getItemValue = function (key, item) { if (item === undefined) return undefined; return item.expiry ? this._getOrDeleteIfExpired(key, item) : item.value; }; QuickLRU.prototype._peek = function (key, cache) { var item = cache.get(key); return this._getItemValue(key, item); }; QuickLRU.prototype._set = function (key, value) { this.cache.set(key, value); this._size++; if (this._size >= this.maxSize) { this._size = 0; this._emitEvictions(this.oldCache); this.oldCache = this.cache; this.cache = new Map(); } }; QuickLRU.prototype._moveToRecent = function (key, item) { this.oldCache.delete(key); this._set(key, item); }; QuickLRU.prototype._entriesAscending = function () { var _a, _b, item, _c, key, value, deleted, e_2_1, _d, _e, item, _f, key, value, deleted, e_3_1; var e_2, _g, e_3, _h; return __generator(this, function (_j) { switch (_j.label) { case 0: _j.trys.push([0, 5, 6, 7]); _a = __values(this.oldCache), _b = _a.next(); _j.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; item = _b.value; _c = __read(item, 2), key = _c[0], value = _c[1]; if (!!this.cache.has(key)) return [3 /*break*/, 3]; deleted = this._deleteIfExpired(key, value); if (!(deleted === false)) return [3 /*break*/, 3]; return [4 /*yield*/, item]; case 2: _j.sent(); _j.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_2_1 = _j.sent(); e_2 = { error: e_2_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_g = _a.return)) _g.call(_a); } finally { if (e_2) throw e_2.error; } return [7 /*endfinally*/]; case 7: _j.trys.push([7, 12, 13, 14]); _d = __values(this.cache), _e = _d.next(); _j.label = 8; case 8: if (!!_e.done) return [3 /*break*/, 11]; item = _e.value; _f = __read(item, 2), key = _f[0], value = _f[1]; deleted = this._deleteIfExpired(key, value); if (!(deleted === false)) return [3 /*break*/, 10]; return [4 /*yield*/, item]; case 9: _j.sent(); _j.label = 10; case 10: _e = _d.next(); return [3 /*break*/, 8]; case 11: return [3 /*break*/, 14]; case 12: e_3_1 = _j.sent(); e_3 = { error: e_3_1 }; return [3 /*break*/, 14]; case 13: try { if (_e && !_e.done && (_h = _d.return)) _h.call(_d); } finally { if (e_3) throw e_3.error; } return [7 /*endfinally*/]; case 14: return [2 /*return*/]; } }); }; /** Get an item. @returns The stored item or `undefined`. */ QuickLRU.prototype.get = function (key) { if (this.cache.has(key)) { var item = this.cache.get(key); return this._getItemValue(key, item); } if (this.oldCache.has(key)) { var item = this.oldCache.get(key); if (this._deleteIfExpired(key, item) === false) { this._moveToRecent(key, item); return item.value; } } }; QuickLRU.prototype.set = function (key, value, _a) { var _b = _a === void 0 ? {} : _a, _c = _b.maxAge, maxAge = _c === void 0 ? this.maxAge : _c; var expiry = typeof maxAge === "number" && maxAge !== Number.POSITIVE_INFINITY ? Date.now() + maxAge : undefined; if (this.cache.has(key)) { this.cache.set(key, { value: value, expiry: expiry, }); } else { this._set(key, { value: value, expiry: expiry }); } }; QuickLRU.prototype.has = function (key) { if (this.cache.has(key)) { return !this._deleteIfExpired(key, this.cache.get(key)); } if (this.oldCache.has(key)) { return !this._deleteIfExpired(key, this.oldCache.get(key)); } return false; }; QuickLRU.prototype.peek = function (key) { if (this.cache.has(key)) { return this._peek(key, this.cache); } if (this.oldCache.has(key)) { return this._peek(key, this.oldCache); } }; QuickLRU.prototype.delete = function (key) { var deleted = this.cache.delete(key); if (deleted) { this._size--; } return this.oldCache.delete(key) || deleted; }; QuickLRU.prototype.clear = function () { this.cache.clear(); this.oldCache.clear(); this._size = 0; }; QuickLRU.prototype.resize = function (maxSize) { if (!(maxSize && maxSize > 0)) { throw new TypeError("`maxSize` must be a number greater than 0"); } var items = __spreadArray([], __read(this._entriesAscending()), false); var removeCount = items.length - maxSize; if (removeCount < 0) { this.cache = new Map(items); this.oldCache = new Map(); this._size = items.length; } else { if (removeCount > 0) { this._emitEvictions(items.slice(0, removeCount)); } this.oldCache = new Map(items.slice(removeCount)); this.cache = new Map(); this._size = 0; } this.maxSize = maxSize; }; QuickLRU.prototype.keys = function () { var _a, _b, _c, key, e_4_1; var e_4, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 5, 6, 7]); _a = __values(this), _b = _a.next(); _e.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; _c = __read(_b.value, 1), key = _c[0]; return [4 /*yield*/, key]; case 2: _e.sent(); _e.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_4_1 = _e.sent(); e_4 = { error: e_4_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_d = _a.return)) _d.call(_a); } finally { if (e_4) throw e_4.error; } return [7 /*endfinally*/]; case 7: return [2 /*return*/]; } }); }; QuickLRU.prototype.values = function () { var _a, _b, _c, value, e_5_1; var e_5, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 5, 6, 7]); _a = __values(this), _b = _a.next(); _e.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; _c = __read(_b.value, 2), value = _c[1]; return [4 /*yield*/, value]; case 2: _e.sent(); _e.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_5_1 = _e.sent(); e_5 = { error: e_5_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_d = _a.return)) _d.call(_a); } finally { if (e_5) throw e_5.error; } return [7 /*endfinally*/]; case 7: return [2 /*return*/]; } }); }; QuickLRU.prototype[Symbol.iterator] = function () { var _a, _b, item, _c, key, value, deleted, e_6_1, _d, _e, item, _f, key, value, deleted, e_7_1; var e_6, _g, e_7, _h; return __generator(this, function (_j) { switch (_j.label) { case 0: _j.trys.push([0, 5, 6, 7]); _a = __values(this.cache), _b = _a.next(); _j.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; item = _b.value; _c = __read(item, 2), key = _c[0], value = _c[1]; deleted = this._deleteIfExpired(key, value); if (!(deleted === false)) return [3 /*break*/, 3]; return [4 /*yield*/, [key, value.value]]; case 2: _j.sent(); _j.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_6_1 = _j.sent(); e_6 = { error: e_6_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_g = _a.return)) _g.call(_a); } finally { if (e_6) throw e_6.error; } return [7 /*endfinally*/]; case 7: _j.trys.push([7, 12, 13, 14]); _d = __values(this.oldCache), _e = _d.next(); _j.label = 8; case 8: if (!!_e.done) return [3 /*break*/, 11]; item = _e.value; _f = __read(item, 2), key = _f[0], value = _f[1]; if (!!this.cache.has(key)) return [3 /*break*/, 10]; deleted = this._deleteIfExpired(key, value); if (!(deleted === false)) return [3 /*break*/, 10]; return [4 /*yield*/, [key, value.value]]; case 9: _j.sent(); _j.label = 10; case 10: _e = _d.next(); return [3 /*break*/, 8]; case 11: return [3 /*break*/, 14]; case 12: e_7_1 = _j.sent(); e_7 = { error: e_7_1 }; return [3 /*break*/, 14]; case 13: try { if (_e && !_e.done && (_h = _d.return)) _h.call(_d); } finally { if (e_7) throw e_7.error; } return [7 /*endfinally*/]; case 14: return [2 /*return*/]; } }); }; /** Iterable for all entries, starting with the newest (descending in recency). */ QuickLRU.prototype.entriesDescending = function () { var items, i, item, _a, key, value, deleted, i, item, _b, key, value, deleted; return __generator(this, function (_c) { switch (_c.label) { case 0: items = __spreadArray([], __read(this.cache), false); i = items.length - 1; _c.label = 1; case 1: if (!(i >= 0)) return [3 /*break*/, 4]; item = items[i]; _a = __read(item, 2), key = _a[0], value = _a[1]; deleted = this._deleteIfExpired(key, value); if (!(deleted === false)) return [3 /*break*/, 3]; return [4 /*yield*/, [key, value.value]]; case 2: _c.sent(); _c.label = 3; case 3: --i; return [3 /*break*/, 1]; case 4: items = __spreadArray([], __read(this.oldCache), false); i = items.length - 1; _c.label = 5; case 5: if (!(i >= 0)) return [3 /*break*/, 8]; item = items[i]; _b = __read(item, 2), key = _b[0], value = _b[1]; if (!!this.cache.has(key)) return [3 /*break*/, 7]; deleted = this._deleteIfExpired(key, value); if (!(deleted === false)) return [3 /*break*/, 7]; return [4 /*yield*/, [key, value.value]]; case 6: _c.sent(); _c.label = 7; case 7: --i; return [3 /*break*/, 5]; case 8: return [2 /*return*/]; } }); }; QuickLRU.prototype.entriesAscending = function () { var _a, _b, _c, key, value, e_8_1; var e_8, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 5, 6, 7]); _a = __values(this._entriesAscending()), _b = _a.next(); _e.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; _c = __read(_b.value, 2), key = _c[0], value = _c[1]; return [4 /*yield*/, [key, value.value]]; case 2: _e.sent(); _e.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_8_1 = _e.sent(); e_8 = { error: e_8_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_d = _a.return)) _d.call(_a); } finally { if (e_8) throw e_8.error; } return [7 /*endfinally*/]; case 7: return [2 /*return*/]; } }); }; Object.defineProperty(QuickLRU.prototype, "size", { get: function () { var e_9, _a; if (!this._size) { return this.oldCache.size; } var oldCacheSize = 0; try { for (var _b = __values(this.oldCache.keys()), _c = _b.next(); !_c.done; _c = _b.next()) { var key = _c.value; if (!this.cache.has(key)) { oldCacheSize++; } } } catch (e_9_1) { e_9 = { error: e_9_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_9) throw e_9.error; } } return Math.min(this._size + oldCacheSize, this.maxSize); }, enumerable: false, configurable: true }); return QuickLRU; }()); exports.QuickLRU = QuickLRU;