@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
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OverwriteAgedDeleteStrategy = void 0;
const AgingCacheWriteStrategy_1 = require("./AgingCacheWriteStrategy");
/**
* Strategy to overwrite only if our value is newer than the high level
*/
class OverwriteAgedDeleteStrategy 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.OverwriteAgedDeleteStrategy = OverwriteAgedDeleteStrategy;