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
JavaScript
;
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 SimpleFIFO = /** @class */ (function (_super) {
__extends(SimpleFIFO, _super);
function SimpleFIFO() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.keyArray = [];
return _this;
}
SimpleFIFO.prototype.get = function (key) {
return this.cache.get(key);
};
SimpleFIFO.prototype.set = function (key, value) {
if (this.keyArray.length >= this._maxObjectsInCache && !this.cache.has(key)) {
this.cache.delete(this.keyArray.shift());
}
this.keyArray.push(key);
return this.cache.set(key, [value, Date.now(), 0]);
};
SimpleFIFO.prototype.clear = function () {
_super.prototype.clear.call(this);
this.keyArray = [];
};
return SimpleFIFO;
}(Cache_1.SimpleCache));
exports.SimpleFIFO = SimpleFIFO;