本文内容为 http://www.redis.io/commands/pubsub 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
PUBSUB subcommand [argument [argument ...]]

命令 PUBSUB 是一条自省命令(Introspection command),可以用它来检测 PUB/SUB 系统的状态。

The PUBSUB command is an introspection command that allows to inspect the state of the Pub/Sub subsystem.

它由一系列子命令组成,其一般形式为:

PUBSUB <subcommand> ... args ...
  • PUBSUB CHANNELS [pattern]

    列出当前所有的活跃频道(Active channel

    Lists the currently active channels.

    一个活跃频道是指拥有一个或多个订阅者的 PUB/SUB (但不包括被匹配模式所订阅的客户端)。

    An active channel is a Pub/Sub channel with one ore more subscribers (not including clients subscribed to patterns).

    如果未指定匹配模式,将列出所有的频道,否则如果指定了匹配模式,仅仅匹配指定的通配模式的频道才会被列出来。

    If no pattern is specified, all the channels are listed, otherwise if pattern is specified only channels matching the specified glob-style pattern are listed.

    返回值

    匹配指定模式的活跃频道列表。

    Array reply: a list of active channels, optionally matching the specified pattern.
  • PUBSUB NUMSUB [channel-1 ... channel-N]

    返回指定频道的订阅者(Subscriber数目(不计算被模式匹配所订阅的客户端)。

    Returns the number of subscribers (not counting clients subscribed to patterns) for the specified channels.

    返回值

    一列频道,及各个频道的订阅者数量。格式为:频道、数目、频道、数目……,也就是说该列表是平坦的(Flat)。对应的频道顺序与命令执行时指定的频道顺序一致。

    Array reply: a list of channels and number of subscribers for every channel. The format is channel, count, channel, count, ..., so the list is flat. The order in which the channels are listed is the same as the order of the channels specified in the command call.

    注意 不带任何频道参数地调用该命令也是有效的。在这种情况下,将会仅仅返回一个空列表。

    Note that it is valid to call this command without channels. In this case it will just return an empty list.
  • PUBSUB NUMPAT

    返回订阅到匹配模式(由命令 PSUBSCRIBE 所执行)的数量。

    Returns the number of subscriptions to patterns (that are performed using the PSUBSCRIBE command).

    注意,这不仅仅是被订阅到某个匹配模式的客户端(Client)的数量,而是所有客户端所被订阅的匹配模式的总数。

    Note that this is not just the count of clients subscribed to patterns but the total number of patterns all the clients are subscribed to.

    返回值

    所有客户端程序所被订阅的匹配模式的总数。

    Integer reply: the number of patterns all the clients are subscribed to.
  • 版本支持

    2.8.0+

    时间复杂度(Time complexity)

    O(N) 用于 CHANNELS 子命令。

    O(N) 用于 NUMSUB 子命令。

    O(1) 用于NUMPAT 子命令。

    O(N) for the CHANNELS subcommand, where N is the number of active channels, and assuming constant time pattern matching (relatively short channels and patterns). O(N) for the NUMSUB subcommand, where N is the number of requested channels. O(1) for the NUMPAT subcommand.