无涯教程-Redis - 发布订阅命令

85 阅读1分钟

Redis Pub/Sub实现了消息传递系统,其中发件人(用redis术语称为发布者)发送消息,而接收者(订户)接收消息,消息传输所通过的链接称为 channel 。

在Redis中,客户端可以订阅任意数量的频道。

Publish Subscribe - 示例

以下示例说明了发布订户概念的工作方式。在以下示例中,一个客户订阅了一个名为" 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 Learnfk point"  
(integer) 1   
1) "message" 
2) "redisChat" 
3) "Redis is a great caching technique" 
1) "message" 
2) "redisChat" 
3) "Learn redis by Learnfk point" 

Publish Subscribe - 命令

下表列出了与Redis发布/订阅相关的一些基本命令。

Sr.No Command & 描述
1 PSUBSCRIBE pattern [pattern ...]

订阅与给定模式匹配的频道。

2 PUBSUB subcommand [argument [argument ...]]

告知发布/订阅系统的状态。如,哪些客户端在服务器上处于活动状态。

3 PUBLISH channel message

将消息发布到频道。

4 PUNSUBSCRIBE [pattern [pattern ...]]

停止监听发布到与给定模式匹配的频道的消息。

5 SUBSCRIBE channel [channel ...]

侦听发布到给定频道的消息。

6 UNSUBSCRIBE [channel [channel ...]]

停止收听发布到给定频道的消息。

参考链接

www.learnfk.com/redis/redis…