@sitecore-jss/sitecore-jss
Version:
This module is provided as a part of Sitecore JavaScript Rendering SDK. It contains the core JSS APIs (layout service) and utilities.
55 lines (54 loc) • 1.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryCacheClient = void 0;
const memory_cache_1 = require("memory-cache");
/**
* Default cache configuration
*/
const 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
*/
class MemoryCacheClient {
/**
* Initializes a new instance of @see MemoryCacheClient using the provided @see CacheOptions
* @param {CacheOptions} options Configuration options
*/
constructor(options) {
var _a;
this.options = options;
this.cache = new memory_cache_1.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.
*/
getCacheValue(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.
*/
setCacheValue(key, value) {
return this.options.cacheEnabled
? this.cache.put(key, value, this.options.cacheTimeout)
: value;
}
}
exports.MemoryCacheClient = MemoryCacheClient;