UNPKG

simple-node-memory-cache

Version:

In-memory object cache written in typescript for Node that supports multiple eviction strategies.

41 lines (40 loc) 1.6 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var Cache_1 = require("../cacheBase/Cache"); var SimpleLIFO = /** @class */ (function (_super) { __extends(SimpleLIFO, _super); function SimpleLIFO() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.keyArray = []; return _this; } SimpleLIFO.prototype.get = function (key) { return this.cache.get(key); }; SimpleLIFO.prototype.set = function (key, value) { if (this.keyArray.length >= this._maxObjectsInCache && !this.cache.has(key)) { this.cache.delete(this.keyArray.pop()); } this.keyArray.push(key); return this.cache.set(key, [value, Date.now(), 0]); }; SimpleLIFO.prototype.clear = function () { _super.prototype.clear.call(this); this.keyArray = []; }; return SimpleLIFO; }(Cache_1.SimpleCache)); exports.SimpleLIFO = SimpleLIFO;