我将 Redis 与 Node.js 结合使用。
client = redis.createClient();
client.subscribe("__keyspace@0__:url_set");
client.on("message",function(channel, message) {
client.on('connect', function() {
client.lrange('url_list',0,-1,function(err3, reply3) {
console.log(reply3);
});
client.smembers('url_set',function(err4, reply4) {
console.log(reply4);
});
});
});
这里reply3
和reply4
是未定义
。 pub sub“on message” block 中不允许使用 lrange()
和 smembers()
吗?
请您参考如下方法:
是的,当客户端处于订阅者模式时,不允许使用常规命令。为常规命令创建另一个客户端。
var client2 = redis.createClient();
使用client2
作为lrange()
和smembers()
。
引用node-redis doc :
When a client issues a SUBSCRIBE or PSUBSCRIBE, that connection is put into a "subscriber" mode. At that point, only commands that modify the subscription set are valid. When the subscription set is empty, the connection is put back into regular mode.
If you need to send regular commands to Redis while in subscriber mode, just open another connection.