cache-manager-memory-store
Version:
Extremely simple in-memory store for node-cache-manager. Primarily used for testing purposes.
119 lines (96 loc) • 2.9 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var MemoryClient = function () {
function MemoryClient(options) {
_classCallCheck(this, MemoryClient);
this.options = options;
this.data = {};
}
_createClass(MemoryClient, [{
key: 'get',
value: function get(key, options, cb) {
if (typeof options === 'function') {
cb = options;
}
cb(null, this.data[key] || null);
}
}, {
key: 'set',
value: function set(key, value, options, cb) {
if (typeof options === 'function') {
cb = options;
}
this.data[key] = value;
cb(null, true);
}
}, {
key: 'del',
value: function del(key, options, cb) {
if (typeof options === 'function') {
cb = options;
}
if (typeof cb !== 'function') {
cb = function cb() {};
}
if (this.data[key]) {
delete this.data[key];
}
cb(null, null);
}
}, {
key: 'reset',
value: function reset(cb) {
if (typeof cb !== 'function') {
cb = function cb() {};
}
this.data = {};
cb(null, null);
}
}, {
key: 'isCacheableValue',
value: function isCacheableValue(value) {
if (this.options.isCacheableValue) {
return this.options.isCacheableValue(value);
}
return value !== null && value !== undefined;
}
}, {
key: 'getClient',
value: function getClient(cb) {
if (!cb) {
return {
client: this
};
}
return cb(null, {
client: this
});
}
}, {
key: 'keys',
value: function keys(pattern, cb) {
var keys = Object.keys(this.data);
var checkPattern = true;
if (typeof pattern === 'function') {
cb = pattern;
checkPattern = false;
}
if (checkPattern) {
var matches = [];
keys.forEach(function (key) {
if (key.includes(pattern)) {
matches.push(key);
}
});
return cb(null, matches);
}
cb(null, keys);
}
}]);
return MemoryClient;
}();
exports.default = MemoryClient;
;