本教程讲解redis教程之发布订阅
Redis 的 pub sub实现了邮件系统,发送者(在 Redis 术语中被称为发布者)发送的邮件,而接收器(用户)接收它们。由该消息传送的链路被称为信道。
Redis客户端可以订阅任何数目的通道。
例子
以下举例说明如何发布用户的概念工作。在下面的例子给出一个客户端订阅一个通道名为redisChat
redis 127.0.0.1:6379> SUBSCRIBE redisChat Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "redisChat" 3) (integer) 1
现在,两个客户端都发布在同一个通道名redisChat消息及以上的订阅客户端接收消息。
redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique" (integer) 1 redis 127.0.0.1:6379> PUBLISH redisChat "Learn redis by tutorials point" (integer) 1 1) "message" 2) "redisChat" 3) "Redis is a great caching technique" 1) "message" 2) "redisChat" 3) "Learn redis by tutorials point"
Redis PubSub 命令
如下表所示相关Redis PubSub的一些基本的命令:
S.N.Command & 描述 1PSUBSCRIBE pattern [pattern ...] 订阅通道匹配给定的模式。 2PUBSUB subcommand [argument [argument ...]] 讲述了PubSub的系统,例如它的客户是活动在服务器上的状态。 3PUBLISH channel message 发布一条消息到通道。 4PUNSUBSCRIBE [pattern [pattern ...]] 停止监听发布到通道匹配给定模式的消息。 5SUBSCRIBE channel [channel ...] 监听发布到指定的通道信息。 6UNSUBSCRIBE [channel [channel ...]] 停止监听发布给定的通道信息。