UNPKG

@linkedmink/multilevel-aging-cache

Version:

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

38 lines (37 loc) 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RefreshAlwaysDeleteStrategy = void 0; const AgingCacheWriteStrategy_1 = require("./AgingCacheWriteStrategy"); /** * Strategy to overwrite only if we're forced to */ class RefreshAlwaysDeleteStrategy extends AgingCacheWriteStrategy_1.AgingCacheWriteStrategy { delete(key, force) { if (force) { return this.executeDelete(key); } return this.deleteConditionally(key); } evict(key, evictAtLevel, force) { if (force) { return this.executeDelete(key, evictAtLevel); } return this.deleteConditionally(key, evictAtLevel); } deleteConditionally(key, evictAtLevel) { return this.hierarchy.getValueAtTopLevel(key).then(highestAgedValue => { if (!highestAgedValue) { return this.executeDelete(key, evictAtLevel); } return this.hierarchy.getValueAtBottomLevel(key).then(lowestAgedValue => { if (lowestAgedValue && this.evictQueue.compare(lowestAgedValue.age, highestAgedValue.age) === 0) { return this.executeDelete(key, evictAtLevel); } this.logger.debug(`Delete deferred: key=${key},ageToSet=${lowestAgedValue ? lowestAgedValue.age : 'null'},ageFound=${highestAgedValue.age}`); return this.setFromHighestLevel(key, highestAgedValue); }); }); } } exports.RefreshAlwaysDeleteStrategy = RefreshAlwaysDeleteStrategy;