@linkedmink/multilevel-aging-cache
Version:
Package provides an interface to cache and persist data to Redis, MongoDB, memory
38 lines (37 loc) • 1.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RefreshAlwaysSetStrategy = void 0;
const AgingCacheWriteStrategy_1 = require("./AgingCacheWriteStrategy");
/**
* Strategy to overwrite only if we're forced to
*/
class RefreshAlwaysSetStrategy 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) {
return this.hierarchy.getValueAtTopLevel(key).then(highestAgedValue => {
if (!highestAgedValue) {
return this.executeSet(key, value, evictAtLevel);
}
return this.hierarchy.getValueAtBottomLevel(key).then(lowestAgedValue => {
if (lowestAgedValue &&
this.evictQueue.compare(lowestAgedValue.age, highestAgedValue.age) === 0) {
return this.executeSet(key, value, evictAtLevel);
}
this.logger.debug(`Delete deferred: key=${key},ageToSet=${lowestAgedValue ? lowestAgedValue.age : 'null'},ageFound=${highestAgedValue.age}`);
return this.setFromHighestLevel(key, highestAgedValue);
});
});
}
}
exports.RefreshAlwaysSetStrategy = RefreshAlwaysSetStrategy;