layered-loader
Version:
Data loader with support for caching and fallback data sources
46 lines • 1.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisNotificationConsumer = void 0;
const AbstractNotificationConsumer_1 = require("../notifications/AbstractNotificationConsumer");
class RedisNotificationConsumer extends AbstractNotificationConsumer_1.AbstractNotificationConsumer {
redis;
channel;
constructor(redis, config) {
super(config.serverUuid);
this.redis = redis;
this.channel = config.channel;
}
async close() {
await this.redis.unsubscribe(this.channel);
return new Promise((resolve) => {
void this.redis.quit((_err, result) => {
return resolve();
});
});
}
subscribe() {
return this.redis.subscribe(this.channel).then(() => {
this.redis.on('message', (channel, message) => {
const parsedMessage = JSON.parse(message);
// this is a local message, ignore
if (parsedMessage.originUuid === this.serverUuid) {
return;
}
if (parsedMessage.actionId === 'DELETE') {
return this.targetCache.delete(parsedMessage.key);
}
if (parsedMessage.actionId === 'DELETE_MANY') {
return this.targetCache.deleteMany(parsedMessage.keys);
}
if (parsedMessage.actionId === 'CLEAR') {
return this.targetCache.clear();
}
if (parsedMessage.actionId === 'SET') {
return this.targetCache.set(parsedMessage.key, parsedMessage.value);
}
});
});
}
}
exports.RedisNotificationConsumer = RedisNotificationConsumer;
//# sourceMappingURL=RedisNotificationConsumer.js.map
;