Redis键空间事件未触发

我有两个Redis客户端,在一个文件中,我有一个简单的脚本设置并删除了Redis密钥:

var redis = require("redis");

var client = redis.createClient('6379','127.0.0.1');

client.config("SET","notify-keyspace-events", "KEA");

client.set("string key 1", "string val", redis.print);

client.set("string key 2", "string val", redis.print);

client.set("placeholder","placeholder value", redis.print);

client.del("string key 1", redis.print);

client.del("string key", redis.print);

在第二个文件中,我有一个Redis客户端充当订户:

var redis = require("redis");

var subscriber = redis.createClient('6379','127.0.0.1');

const REDIS_PUB_MESSAGE = 'redis_pub_message';

const EVENT_SET = '__keyevent@0__:set';

const EVENT_DEL = '__keyevent@0__:del';

const SPACE_SPECIFIC_KEY = '__keyspace@0__:placeholder set';

const EVENT_SPECIFIC_KEY = '__keyevent@0__:placeholder set';

const SPACE_SPECIFIC_KEY_set = '__keyspace@0__:set placeholder';

const EVENT_SPECIFIC_KEY_set = '__keyevent@0__:set placeholder';

subscriber.on('message', function(channel, key) {

switch (channel) {

case SPACE_SPECIFIC_KEY_set:

console.log('space specific key channel:',channel,'key:',key);

case EVENT_SPECIFIC_KEY_set:

console.log('event specific key channel:',channel,'key:',key);

case EVENT_SPECIFIC_KEY:

console.log('space specific key channel:',channel,'key:',key);

case SPACE_SPECIFIC_KEY:

console.log('event specific key channel:',channel,'key:',key);

}

});

关键的“占位符”已设置,那么是否有充分的理由使我在“消息”处理程序中未获得任何输出?

回答:

您忘记了订阅用户客户端订阅特定的频道。此外,如果要监视所有事件,则需要使用基于模式的订阅。

您可能想要执行以下操作(未测试):

subscriber.on("pmessage", function (pattern, channel, message) {

console.log("("+ pattern +")" + " client received message on " + channel + ": " + message);

switch (channel) {

// blah blah blah

// ...

}

});

subscriber.psubscribe(''__key*__:*')

请参阅Redis文档和node_redis示例中的更多信息。

这是一个示例,说明了频道订阅和模式订阅之间的区别。为了简洁起见,省略了正确的错误处理。

var redis = require("redis");

var client = redis.createClient('6379','127.0.0.1');

var subscriber1 = redis.createClient('6379','127.0.0.1');

var subscriber2 = redis.createClient('6379','127.0.0.1');

// First subscriber listens only to events occurring for key mykey

function S1(next) {

subscriber1.on('message', function(channel, msg) {

console.log( "S1: received on "+channel+" event "+msg )

});

subscriber1.subscribe( "__keyspace@0__:mykey", function (err) {

next();

});

}

// Second subscriber listens to events occuring for ALL keys

function S2(next) {

subscriber2.on('pmessage', function(pattern,channel, msg) {

console.log( "S2: received on "+channel+" event "+msg )

});

subscriber2.psubscribe( "__keyspace@0__:*", function (err) {

next();

});

}

// Do something with keys mykey and anotherkey

function do_something() {

client.set("mykey","example", function( err ) {

client.set("mykey", "another example", function( err ) {

client.del("mykey", function( err ) {

client.set("anotherkey","example", function( err ) {

client.del("anotherkey");

});

});

});

});

}

// Here we go

S1( function () {

S2( function () {

do_something();

});

});

该脚本的结果是:

S1: received on __keyspace@0__:mykey event set

S2: received on __keyspace@0__:mykey event set

S2: received on __keyspace@0__:mykey event set

S1: received on __keyspace@0__:mykey event set

S1: received on __keyspace@0__:mykey event del

S2: received on __keyspace@0__:mykey event del

S2: received on __keyspace@0__:anotherkey event set

S2: received on __keyspace@0__:anotherkey event del

您可以看到第一个订阅者仅接收了mykey事件,而第二个订阅者接收了所有关键字事件。

以上是 Redis键空间事件未触发 的全部内容, 来源链接: utcz.com/qa/405962.html

回到顶部