我正在尝试使用Redis TTL启动延迟过程。
为了最小化问题,我只在一台机器上开始订阅键空间通知。
使用Redis创建连接
createClient() {
if (!this.client) {
var options = {
host: 'host',
port: 'port',
masterName: 'masterName'
}
if (REDIS_AUTH == 1) {
options.master_auth_pass = REDIS_PASS
}
this.client = sentinel.createClient(options);
this.client.on('ready', () => {
logger.info('redis -> createClient -> on ready');
this.client.getMaster().config('SET', 'notify-keyspace-events', 'Ex');
});
this.client.on('switch master', () => {
logger.info('redis -> createClient -> on switch master');
this.client.getMaster().config('SET', 'notify-keyspace-events', 'Ex');
});
}
}
订阅
psubscribe(paramKey) {
this.redis.client = null;
if (!this.clientSubscribe) {
this.redis.createClient();
this.clientSubscribe = this.redis.client;
this.redis.client = null;
}
this.clientSubscribe.on('connect', () => {
this._psubscribe_reconect(paramKey);
});
this.clientSubscribe.on('reconnecting', () => {
this._psubscribe_reconect(paramKey);
});
}
_psubscribe_reconect(paramKey) {
this.clientSubscribe.on('message', this.onMessage.bind(this));
paramKey.forEach(element => {
this.clientSubscribe.subscribe(element);
});
}
收到通知时
onMessage(paramChannel, paramMessage) {
const functionName = 'onMessage';
this.Logger.debug(`Recebeu uma mensagem no canal ${paramChannel}`, paramMessage, this.CLASS_NAME, functionName);
}
当前结果是,有一段时间我收到了多个事件。示例:
[2019-04-12T10:09:57-03:00]:调试Recebeu uma mensagem no canal keyevent @ 0 :已过期REMINDER:DELAY:5cb08b941f0f3f008119f81d RedisSubcriber onMessage [2019-04-12T10:09:57-03:00]:调试Recebeu uma mensagem no canal keyevent @ 0 :expired REMINDER:DELAY:5cb08b941f0f3f008119f81d RedisSubcriber onMessage [2019-04-12T10:09:57-03:00]:调试Recebeu uma mensagem no canal keyevent @ 0 :expired REMINDER:DELAY:5cb08b941f0f3f008119f81d RedisSubcriber onMessage [2019-04-12T10:09:57-03:00]:调试Recebeu uma mensagem no canal keyevent @ 0 :expired REMINDER:DELAY:5cb08b941f0f3f008119f81d RedisSubcriber onMessage [2019-04-12T10:09:57-03:00]:调试Recebeu uma mensagem no canal keyevent @ 0 :expired REMINDER:DELAY:5cb08b941f0f3f008119f81d RedisSubcriber onMessage [2019-04-12T10:09:57-03:00]:调试Recebeu uma mensagem no canal keyevent @ 0 :expired REMINDER:DELAY:5cb08b941f0f3f008119f81d RedisSubcriber onMessage
如何解决这个问题?