simple-node-memory-cache
Version:
In-memory object cache written in typescript for Node that supports multiple eviction strategies.
42 lines (41 loc) • 1.67 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 SimpleRR = /** @class */ (function (_super) {
__extends(SimpleRR, _super);
function SimpleRR() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.keyArray = [];
return _this;
}
SimpleRR.prototype.get = function (key) {
return this.cache.get(key);
};
SimpleRR.prototype.set = function (key, value) {
if (this.keyArray.length >= this._maxObjectsInCache && !this.cache.has(key)) {
var itemToRemove = Math.floor(Math.random() * 5);
this.cache.delete(this.keyArray.splice(itemToRemove, 1)[0]);
}
this.keyArray.push(key);
return this.cache.set(key, [value, Date.now(), 0]);
};
SimpleRR.prototype.clear = function () {
_super.prototype.clear.call(this);
this.keyArray = [];
};
return SimpleRR;
}(Cache_1.SimpleCache));
exports.SimpleRR = SimpleRR;