@jss-rule-engine/edge
Version:
53 lines (52 loc) • 1.95 kB
JavaScript
import { Cache } from 'memory-cache';
/**
* Default cache configuration
*/
var DEFAULTS = Object.freeze({
cacheTimeout: 60,
cacheEnabled: true,
});
/**
* A cache client that uses the 'memory-cache' library (https://github.com/ptarjan/node-cache).
* This class is meant to be extended or used as a mixin; it's not meant to be used directly.
* @template T The type of data being cached.
* @mixin
*/
var MemoryCacheClient = /** @class */ (function () {
/**
* Initializes a new instance of @see MemoryCacheClient using the provided @see CacheOptions
* @param {CacheOptions} options Configuration options
*/
function MemoryCacheClient(options) {
var _a;
this.options = options;
this.cache = new Cache();
this.options.cacheTimeout = ((_a = this.options.cacheTimeout) !== null && _a !== void 0 ? _a : DEFAULTS.cacheTimeout) * 1000;
if (this.options.cacheEnabled === undefined) {
this.options.cacheEnabled = DEFAULTS.cacheEnabled;
}
}
/**
* Retrieves a value from the cache.
* @template T The type of data being cached.
* @param {string} key The cache key.
* @returns The cache value as {T}, or null if the specified key is not found in the cache.
*/
MemoryCacheClient.prototype.getCacheValue = function (key) {
return this.options.cacheEnabled ? this.cache.get(key) : null;
};
/**
* Adds a value to the cache for the specified cache key.
* @template T The type of data being cached.
* @param {string} key The cache key.
* @param {T} value The value to cache.
* @returns The value added to the cache.
*/
MemoryCacheClient.prototype.setCacheValue = function (key, value) {
return this.options.cacheEnabled
? this.cache.put(key, value, this.options.cacheTimeout)
: value;
};
return MemoryCacheClient;
}());
export { MemoryCacheClient };