Redis指令详解-发布订阅相关指令

目录

目标

PUBLISH

PUBSUB

    PUBSUB CHANNELS [pattern]

    PUBSUB NUMSUB [channel-1 ... channel-N]

    PUBSUB NUMPAT

SUBSCRIBE

UNSUBSCRIBE

PSUBSCRIBE

PUNSUBSCRIBE


目标

列举redis中与pub/sub相关的指令,包括作用、使用时需要注意的地方等。作为消息队列,所有订阅者都将收到数据,允许明确订阅和模糊订阅。

PUBLISH

    PUBLISH channel message
    O(N+M) N:订阅该channel的client数量 M:订阅pattern的数量
    向channel发送message,返回接收到该消息的client数量

PUBSUB

    PUBSUB subcommand [argument [argument ...]]
    用于检测redis pub/sub系统的状态

    PUBSUB CHANNELS [pattern]

        列举符合pattern的活跃channel,将至少具有一个订阅者的channel称为活跃channel,不包括订阅patterns的client
        如果未提供pattern,将列举所有channel

    PUBSUB NUMSUB [channel-1 ... channel-N]

        列举channel的订阅者数量
        若未提供channel,则返回empty list

    PUBSUB NUMPAT

        返回被订阅的patterns的数量

SUBSCRIBE

    SUBSCRIBE channel [channel ...]
    让client去订阅channels
    client一旦进入订阅状态,client将仅接受pub/sub相关命令,不支持其他命令。
    举例

subscribe testChannel
// 订阅成功
Switch to Pub/Sub mode. Close console tab to stop listen for messages.
 1)  "subscribe"
 2)  "testChannel"
 3)  "1"
// 接受到的信息
 1)  "message"
 2)  "testChannel" // 来自哪个channel
 3)  "111"             // 接受信息


UNSUBSCRIBE

    UNSUBSCRIBE [channel [channel ...]]
    client注销对channels的订阅,若未提供channel,则针对之前订阅的所有channel进行注销订阅。

PSUBSCRIBE

    PSUBSCRIBE pattern [pattern ...]
    client订阅patterns,针对之后新增channel且name符合pattern时,新channel也将被client订阅。
    pattern举例
        h?llo subscribes to hello, hallo and hxllo
        h*llo subscribes to hllo and heeeello
        h[ae]llo subscribes to hello and hallo, but not hillo
    举例

psubscribe t*
// 订阅成功
Switch to Pub/Sub mode. Close console tab to stop listen for messages.
 1)  "psubscribe"
 2)  "t*"
 3)  "1"
// 收到信息
 1)  "pmessage"
 2)  "t*"
 3)  "testa" // 来自哪个channel
 4)  "1"      // 数据

 1)  "pmessage"
 2)  "t*"
 3)  "testChannel"
 4)  "112"

 1)  "pmessage"
 2)  "t*"
 3)  "test1"
 4)  "1"


PUNSUBSCRIBE

    PUNSUBSCRIBE [pattern [pattern ...]]
    client注销对pattern的订阅
    如果未提供pattern,则针对之前订阅的pattern,进行注销订阅。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值