slim-cache
Version:
Super Fast Lightweight Cache with a Map like interface
97 lines • 2.39 kB
JavaScript
;
/*!
* slim-cache <https://github.com/nivrith/slim-cache>
*
* Copyright (c) Nivrith Mandayam Gomatam
* Licensed under the MIT License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var SlimCache = /** @class */ (function () {
/**
*Creates an instance of SlimCache.
* @param {*} cache Preferably an Object Without Prototype
* @memberof SlimCache
*/
function SlimCache(cache) {
if (cache === void 0) { cache = {}; }
this.cache = cache;
}
/**
*
*
* @param {keyof any} key
* @returns
* @memberof SlimCache
*/
SlimCache.prototype.has = function (key) {
return (typeof this.cache[key] !== 'undefined');
};
/**
*
*
* @param {keyof any} key
* @param {*} value
* @memberof SlimCache
*/
SlimCache.prototype.set = function (key, value) {
this.cache[key] = value;
};
/**
*
*
* @param {keyof any} key
* @returns {*}
* @memberof SlimCache
*/
SlimCache.prototype.get = function (key) {
return this.cache[key];
};
/**
*
*
* @param {keyof any} key
* @returns {Boolean} Returns `true` if the entry was removed successfully, else `false`.
* @memberof SlimCache
*/
SlimCache.prototype.clear = function (key) {
return this.has(key) && delete this.cache[key];
};
/** Replace internal map cache with a fresh one
*
*
* @param {*} [cache=Object.create(null)]
* @memberof SlimCache
*/
SlimCache.prototype.flush = function (cache) {
if (cache === void 0) { cache = Object.create(null); }
this.cache = cache;
};
/**
*
*
* @memberof SlimCache
*/
SlimCache.prototype.clean = function () {
for (var key in this.cache) {
// this check can be safely omitted in modern JS engines
// if (this.cache.hasOwnProperty(key))
delete this.cache[key];
}
};
/**
*
*
* @returns {boolean}
* @memberof SlimCache
*/
SlimCache.prototype.isEmpty = function () {
for (var prop in this.cache) {
return false;
}
return true;
};
return SlimCache;
}());
exports.SlimCache = SlimCache;
exports.default = SlimCache;
//# sourceMappingURL=index.js.map