UNPKG

@linkedmink/multilevel-aging-cache

Version:

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

33 lines (32 loc) 1.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OverwriteAgedSetStrategy = void 0; const AgingCacheWriteStrategy_1 = require("./AgingCacheWriteStrategy"); /** * Strategy to overwrite only if our value is newer than the high level */ class OverwriteAgedSetStrategy extends AgingCacheWriteStrategy_1.AgingCacheWriteStrategy { set(key, value, force) { if (force) { return this.executeSet(key, value); } return this.setConditionally(key, value); } load(key, value, evictAtLevel, force) { if (force) { return this.executeSet(key, value, evictAtLevel); } return this.setConditionally(key, value, evictAtLevel); } setConditionally(key, value, evictAtLevel) { const currentAge = this.evictQueue.getInitialAge(key); return this.hierarchy.getValueAtTopLevel(key).then(highestAgedValue => { if (!highestAgedValue || this.evictQueue.compare(highestAgedValue.age, currentAge) <= 0) { return this.executeSet(key, value, evictAtLevel); } this.logger.debug(`Set deferred: key=${key},ageToSet=${currentAge},ageFound=${highestAgedValue.age}`); return this.setFromHighestLevel(key, highestAgedValue); }); } } exports.OverwriteAgedSetStrategy = OverwriteAgedSetStrategy;