transitory
Version:
In-memory cache with high hit rates via LFU eviction. Supports time-based expiration, automatic loading and metrics.
215 lines • 7.27 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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.WrappedCache = void 0;
var AbstractCache_1 = require("./AbstractCache");
var symbols_1 = require("./symbols");
var PARENT = Symbol('parent');
var REMOVAL_LISTENER = Symbol('removalListener');
/**
* Wrapper for another cache, used to extend that cache with new behavior,
* like for loading things or collecting metrics.
*/
var WrappedCache = /** @class */ (function (_super) {
__extends(WrappedCache, _super);
function WrappedCache(parent, removalListener) {
var _this = _super.call(this) || this;
_this[PARENT] = parent;
_this[REMOVAL_LISTENER] = removalListener;
// Custom onRemove handler for the parent cache
_this[PARENT][symbols_1.ON_REMOVE] = _this[symbols_1.TRIGGER_REMOVE].bind(_this);
return _this;
}
Object.defineProperty(WrappedCache.prototype, "maxSize", {
/**
* The maximum size the cache can be. Will be -1 if the cache is unbounded.
*
* @returns
* maximum size
*/
get: function () {
return this[PARENT].maxSize;
},
enumerable: false,
configurable: true
});
Object.defineProperty(WrappedCache.prototype, "size", {
/**
* The current size of the cache.
*
* @returns
* current size
*/
get: function () {
return this[PARENT].size;
},
enumerable: false,
configurable: true
});
Object.defineProperty(WrappedCache.prototype, "weightedSize", {
/**
* The size of the cache weighted via the activate estimator.
*
* @returns
* current weighted size
*/
get: function () {
return this[PARENT].weightedSize;
},
enumerable: false,
configurable: true
});
/**
* Store a value tied to the specified key. Returns the previous value or
* `null` if no value currently exists for the given key.
*
* @param key -
* key to store value under
* @param value -
* value to store
* @returns
* current value or `null`
*/
WrappedCache.prototype.set = function (key, value) {
return this[PARENT].set(key, value);
};
/**
* Get the cached value for the specified key if it exists. Will return
* the value or `null` if no cached value exist. Updates the usage of the
* key.
*
* @param key -
* key to get
* @returns
* current value or `null`
*/
WrappedCache.prototype.getIfPresent = function (key) {
return this[PARENT].getIfPresent(key);
};
/**
* Peek to see if a key is present without updating the usage of the
* key. Returns the value associated with the key or `null` if the key
* is not present.
*
* In many cases `has(key)` is a better option to see if a key is present.
*
* @param key -
* the key to check
* @returns
* value associated with key or `null`
*/
WrappedCache.prototype.peek = function (key) {
return this[PARENT].peek(key);
};
/**
* Check if the given key exists in the cache.
*
* @param key -
* key to check
* @returns
* `true` if value currently exists, `false` otherwise
*/
WrappedCache.prototype.has = function (key) {
return this[PARENT].has(key);
};
/**
* Delete a value in the cache. Returns the deleted value or `null` if
* there was no value associated with the key in the cache.
*
* @param key -
* the key to delete
* @returns
* deleted value or `null`
*/
WrappedCache.prototype.delete = function (key) {
return this[PARENT].delete(key);
};
/**
* Clear the cache removing all of the entries cached.
*/
WrappedCache.prototype.clear = function () {
this[PARENT].clear();
};
/**
* Get all of the keys in the cache as an array. Can be used to iterate
* over all of the values in the cache, but be sure to protect against
* values being removed during iteration due to time-based expiration if
* used.
*
* @returns
* snapshot of keys
*/
WrappedCache.prototype.keys = function () {
return this[PARENT].keys();
};
/**
* Request clean up of the cache by removing expired entries and
* old data. Clean up is done automatically a short time after sets and
* deletes, but if your cache uses time-based expiration and has very
* sporadic updates it might be a good idea to call `cleanUp()` at times.
*
* A good starting point would be to call `cleanUp()` in a `setInterval`
* with a delay of at least a few minutes.
*/
WrappedCache.prototype.cleanUp = function () {
this[PARENT].cleanUp();
};
Object.defineProperty(WrappedCache.prototype, "metrics", {
/**
* Get metrics for this cache. Returns an object with the keys `hits`,
* `misses` and `hitRate`. For caches that do not have metrics enabled
* trying to access metrics will throw an error.
*
* @returns
* metrics
*/
get: function () {
return this[PARENT].metrics;
},
enumerable: false,
configurable: true
});
Object.defineProperty(WrappedCache.prototype, symbols_1.ON_MAINTENANCE, {
get: function () {
return this[PARENT][symbols_1.ON_MAINTENANCE];
},
enumerable: false,
configurable: true
});
Object.defineProperty(WrappedCache.prototype, symbols_1.ON_MAINTENANCE, {
set: function (listener) {
this[PARENT][symbols_1.ON_MAINTENANCE] = listener;
},
enumerable: false,
configurable: true
});
WrappedCache.prototype[symbols_1.TRIGGER_REMOVE] = function (key, value, reason) {
// Trigger any extended remove listeners
var onRemove = this[symbols_1.ON_REMOVE];
if (onRemove) {
onRemove(key, value, reason);
}
// Trigger the removal listener
var listener = this[REMOVAL_LISTENER];
if (listener) {
listener(key, value, reason);
}
};
return WrappedCache;
}(AbstractCache_1.AbstractCache));
exports.WrappedCache = WrappedCache;
//# sourceMappingURL=WrappedCache.js.map