UNPKG

@linkedmink/multilevel-aging-cache

Version:

Package provides an interface to cache and persist data to Redis, MongoDB, memory

84 lines (83 loc) 4.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAgingCache = exports.checkAgingCacheOptionsValid = void 0; const path_1 = __importDefault(require("path")); const IAgingCacheOptions_1 = require("./cache/IAgingCacheOptions"); const RefreshAlwaysSetStrategy_1 = require("./cache/RefreshAlwaysSetStrategy"); const OverwriteAlwaysSetStrategy_1 = require("./cache/OverwriteAlwaysSetStrategy"); const OverwriteAgedSetStrategy_1 = require("./cache/OverwriteAgedSetStrategy"); const RefreshAlwaysDeleteStrategy_1 = require("./cache/RefreshAlwaysDeleteStrategy"); const OverwriteAlwaysDeleteStrategy_1 = require("./cache/OverwriteAlwaysDeleteStrategy"); const OverwriteAgedDeleteStrategy_1 = require("./cache/OverwriteAgedDeleteStrategy"); const Logger_1 = require("./shared/Logger"); const AgingCache_1 = require("./cache/AgingCache"); const FIFOAgedQueue_1 = require("./queue/FIFOAgedQueue"); const agedQueueMap = new Map([ [IAgingCacheOptions_1.AgingCacheReplacementPolicy.FIFO, FIFOAgedQueue_1.FIFOAgedQueue], ]); const setStrategyMap = new Map([ [IAgingCacheOptions_1.AgingCacheWriteMode.RefreshAlways, RefreshAlwaysSetStrategy_1.RefreshAlwaysSetStrategy], [IAgingCacheOptions_1.AgingCacheWriteMode.OverwriteAlways, OverwriteAlwaysSetStrategy_1.OverwriteAlwaysSetStrategy], [IAgingCacheOptions_1.AgingCacheWriteMode.OverwriteAged, OverwriteAgedSetStrategy_1.OverwriteAgedSetStrategy], ]); const deleteStrategyMap = new Map([ [IAgingCacheOptions_1.AgingCacheWriteMode.RefreshAlways, RefreshAlwaysDeleteStrategy_1.RefreshAlwaysDeleteStrategy], [IAgingCacheOptions_1.AgingCacheWriteMode.OverwriteAlways, OverwriteAlwaysDeleteStrategy_1.OverwriteAlwaysDeleteStrategy], [IAgingCacheOptions_1.AgingCacheWriteMode.OverwriteAged, OverwriteAgedDeleteStrategy_1.OverwriteAgedDeleteStrategy], ]); function checkAgingCacheOptionsValid(options, maxLevel) { if (options.maxEntries !== undefined && options.maxEntries < 1) { return new Error(`maxEntries(${options.maxEntries}): must be greater than 0`); } if (options.ageLimit && options.replacementPolicy === IAgingCacheOptions_1.AgingCacheReplacementPolicy.FIFO && options.ageLimit * 60 <= options.purgeInterval) { return new Error(`maxAge(${options.ageLimit} min): must be greater than purgeInterval(${options.purgeInterval} sec)`); } if (options.purgeInterval < 10) { return new Error(`purgeInterval(${options.purgeInterval}): must be greater than 10 seconds`); } if (options.evictAtLevel && (options.evictAtLevel < 0 || options.evictAtLevel > maxLevel)) { return new Error(`evictAtLevel must be a between 0 and ${maxLevel}`); } } exports.checkAgingCacheOptionsValid = checkAgingCacheOptionsValid; /** * Create a new instance of IAgingCache. This function is a factory that will construct the * corrent implementation based on the provided options. * @param hierarchy The storage hierarchy with the level index 0 being the lowest level * @param options Options for the behavior of the cache, if undefined use getDefaultAgingCacheOptions */ function createAgingCache(hierarchy, options) { if (!options) { options = (0, IAgingCacheOptions_1.getDefaultAgingCacheOptions)(); } else { const validationError = checkAgingCacheOptionsValid(options, hierarchy.totalLevels - 1); if (validationError) { const logger = Logger_1.Logger.get(path_1.default.basename(__filename)); logger.error(validationError.message); throw validationError; } } const agedQueueConstructor = agedQueueMap.get(options.replacementPolicy); if (!agedQueueConstructor) { throw new Error(`Unsupported replacementPolicy: ${options.replacementPolicy}`); } const setStrategyConstructor = setStrategyMap.get(options.setMode); if (!setStrategyConstructor) { throw new Error(`Unsupported setMode: ${options.setMode}`); } const deleteStrategyConstructor = deleteStrategyMap.get(options.deleteMode); if (!deleteStrategyConstructor) { throw new Error(`Unsupported deleteMode: ${options.deleteMode}`); } const evictQueue = new agedQueueConstructor(options.maxEntries, options.ageLimit); const setStrategy = new setStrategyConstructor(hierarchy, evictQueue); const deleteStrategy = new deleteStrategyConstructor(hierarchy, evictQueue); return new AgingCache_1.AgingCache(hierarchy, evictQueue, setStrategy, deleteStrategy, options.evictAtLevel, options.purgeInterval); } exports.createAgingCache = createAgingCache;