Redis PUBSUB命令

Redis PUBSUB命令教程

Redis 的 PUBSUB 是一个查看订阅与发布系统状态的内省命令, 它由数个不同格式的子命令组成。

Redis PUBSUB命令详解

语法

192.168.98.70:6379> PUBSUB <subcommand> [argument [argument ...]]

参数

参数 描述
subcommand 子命令。
argument 参数。

可用版本

>= 2.8.0

Redis PUBSUB子命令一

定义

列出当前的活跃频道。

语法

192.168.98.70:6379> PUBSUB CHANNELS [pattern]

返回值

一个由活跃频道组成的列表。

时间复杂度

O(N) , N 为活跃频道的数量(对于长度较短的频道和模式来说,将进行模式匹配的复杂度视为常数)。

说明

活跃频道指的是那些至少有一个订阅者的频道, 订阅模式的客户端不计算在内。pattern 参数是可选的:

  • 如果不给出 pattern 参数,那么列出订阅与发布系统中的所有活跃频道。
  • 如果给出 pattern 参数,那么只列出和给定模式 pattern 相匹配的那些活跃频道。

案例

获取活跃频道

使用 PUBSUB CHANNELS 命令,获取活跃的频道

# client-1 订阅 module.html 频道 和 module.js 频道 192.168.98.70:6379> SUBSCRIBE module.html module.js Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "module.html" 3) (integer) 1 1) "subscribe" 2) "module.js" 3) (integer) 2 # client-2 订阅 module.html 频道 和 module.tool 频道 192.168.98.70:6379> SUBSCRIBE module.html module.tool Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "module.html" 3) (integer) 1 1) "subscribe" 2) "module.tool" 3) (integer) 2 # client-3 获取活跃的频道 192.168.98.70:6379> PUBSUB CHANNELS 1) "module.tool" 2) "module.js" 3) "module.html" # 获取符合模式 module.h* 的频道 192.168.98.70:6379> PUBSUB CHANNELS module.h* 1) "module.html"

我们首先,使用 SUBSCRIBE 订阅 module.html 频道 和 module.js 频道。此时,打开另一个终端,使用 SUBSCRIBE 订阅 module.html 频道 和 module.tool 频道。

再打开一个终端,使用 PUBSUB CHANNELS 获取活跃的频道。最后,我们再次使用 PUBSUB CHANNELS module.h* 获取符合模式 module.h* 的频道。

Redis PUBSUB子命令二

定义

返回给定频道的订阅者数量, 订阅模式的客户端不计算在内。

语法

192.168.98.70:6379> PUBSUB NUMSUB [channel-1 ... channel-N]

返回值

一个多条批量回复(Multi-bulk reply),回复中包含给定的频道,以及频道的订阅者数量。

格式为:频道 channel-1 , channel-1 的订阅者数量,频道 channel-2 , channel-2 的订阅者数量,诸如此类。

回复中频道的排列顺序和执行命令时给定频道的排列顺序一致。 不给定任何频道而直接调用这个命令也是可以的, 在这种情况下, 命令只返回一个空列表。

时间复杂度

O(N) , N 为给定频道的数量。

案例

获取给定频道的订阅者数量

使用 PUBSUB NUMSUB 命令,获取给定频道的订阅者数量

# client-1 订阅 module.html 频道 和 module.js 频道 192.168.98.70:6379> SUBSCRIBE module.html module.js Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "module.html" 3) (integer) 1 1) "subscribe" 2) "module.js" 3) (integer) 2 # client-2 订阅 module.html 频道 和 module.tool 频道 192.168.98.70:6379> SUBSCRIBE module.html module.tool Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "module.html" 3) (integer) 1 1) "subscribe" 2) "module.tool" 3) (integer) 2 # client-3 192.168.98.70:6379> PUBSUB NUMSUB module.html module.js module.tool module.server 1) "module.html" 2) (integer) 2 3) "module.js" 4) (integer) 1 5) "module.tool" 6) (integer) 1 7) "module.server" 8) (integer) 0

我们首先,使用 SUBSCRIBE 订阅 module.html 频道 和 module.js 频道。此时,打开另一个终端,使用 SUBSCRIBE 订阅 module.html 频道 和 module.tool 频道。

最后,我们再打开一个终端,使用 PUBSUB NUMSUB 获取相应频道的订阅数,频道 module.html 的订阅数为 2,频道 module.js 的订阅数为 1,频道 module.tool 的订阅数为 1,频道 module.server 的订阅数为 0。

Redis PUBSUB子命令三

定义

返回订阅模式的数量。注意, 这个命令返回的不是订阅模式的客户端的数量, 而是客户端订阅的所有模式的数量总和。

语法

192.168.98.70:6379> PUBSUB NUMPAT

返回值

一个整数回复(Integer reply)。

时间复杂度

O(1)

案例

获取订阅模式的数量

使用 PUBSUB NUMPAT 命令,获取订阅模式的数量

# client-1 订阅 module.* 和 category.* 频道 192.168.98.70:6379> PSUBSCRIBE module.* category.* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "module.*" 3) (integer) 1 1) "psubscribe" 2) "category.*" 3) (integer) 2 # client-2 订阅 module.* 频道和 article.* 频道 192.168.98.70:6379> PSUBSCRIBE module.* article.* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "module.*" 3) (integer) 1 1) "psubscribe" 2) "article.*" 3) (integer) 2 # client-3 获取订阅模式的数量 192.168.98.70:6379> PUBSUB NUMPAT (integer) 4

我们首先,使用 PSUBSCRIBE 同时订阅以 module. 开头和以 category. 开头的队列。使用 PSUBSCRIBE 同时订阅以 module. 开头和以 article. 开头的队列。

最后,使用 PUBSUB NUMPAT 获取被订阅的频道的数量,返回 4 ,即 4 个频道被订阅,重复的模式 module.* 被重复订阅了,所以算了两次。

Redis PUBSUB命令总结

Redis 的 PUBSUB 是一个查看订阅与发布系统状态的内省命令, 它由数个不同格式的子命令组成。Redis PUBSUB 命令语法:

192.168.98.70:6379> PUBSUB <subcommand> [argument [argument ...]]