Redis 快速入门 -- Redis 发布订阅(12)

Redis 的pub sub 实现了邮件系统,发送者(在 Redis 术语中被称为发布者)发送的邮件,而接收器(用户)接收它们。由该消息传送得而链路被称为信道。

Redis 客户端可以订阅任何数目的通道。

例子

以下举例说明如果发布用户的概念工作。在下面的例子给出一个客户端订阅一个通道名为 redisChat

redis 127.0.0.1:6379> SUBSCRIBE redisChat

Reading message ... (press Ctrl-c to quit)

1)"subscribe"

2)"redisChat"

3)(integer) 1

现在,两个客户端都发布在同一个通道名 redisChat 消息及以上的订阅客户端接收消息。

redis 127.0.0.1:6379> PBLISH 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 cacheing technique"

1)"message"

2)"redisChat""

3)"Learn redis by tutorials point"

Redis PubSub 命令

如下表所示相关 Redis PubSub 的一些基本的命令:

S.N. Command & 描述
1

PSUBSCRIBE pattern [pattern ...]

订阅通道匹配给定的模式

2

PUBSUB subcommand [argument [argument ...]]

讲述了 PubSub 的系统,例如它的客户是活动在服务器上的状态

3

PUBLISH channel message

发布一条消息到通道

4

PUBSUBSCRIBE [pattern [pattern ...]]

停止监听发不到通道匹配给定模式的消息

5

SUBSCRIBE channel [channel ...]

监听发布到指定的通道信息

6

UNSUBSCRIBE [channel [channel ...]]

停止监听发布给定的通道信息

原文地址:https://www.cnblogs.com/wangjp/p/6022582.html