Linux下数据库学习过程之Redis数据库的发布和订阅(持续更新中)

发布(publish)和订阅(subscribe)

  • Redis 发布订阅 (pub/sub) 是一种消息通信模式:发送者 (pub) 发送消息,订阅者 (sub) 接收消息。Redis 客户端可以订阅任意数量的频道
    发布者
发布信息
127.0.0.1:6379> PUBLISH mychannel 'hello world'
(integer) 0
连续发布
127.0.0.1:6379> PUBLISH mychannel 'hello world'
(integer) 2
127.0.0.1:6379> PUBLISH mychannel 'hello world'
(integer) 3

订阅者(这里我创建了三个订阅者,即开了三个会话)

订阅信息,并且同时处于阻塞状态
127.0.0.1:6379(subscribed mode)> psubscribe mychannel
1) "pmessage"
2) "mychannel"
3) "mychannel"
4) "hello world"
1) "psubscribe"
2) "mychannel"
3) (integer) 1
在发布者发布内容后更新
1) "pmessage"
2) "mychannel"
3) "mychannel"
4) "hello world"

订阅者退订
127.0.0.1:6379> unsubscribe mychannel
1) "unsubscribe"
2) "mychannel"
3) (integer) 0

PUBSUB命令解释(待补充)

127.0.0.1:6379> PUBSUB help
 1) PUBSUB <subcommand> [<arg> [value] [opt] ...]. Subcommands are:
 2) CHANNELS [<pattern>]
 3)     Return the currently active channels matching a <pattern> (default: '*').
 4) NUMPAT(给定一个模式)
 5)     Return number of subscriptions to patterns.
 6) NUMSUB [<channel> ...]
 7)     Return the number of subscribers for the specified channels, excluding
 8)     pattern subscriptions(default: no channels).
 9) SHARDCHANNELS [<pattern>](分片)
10)     Return the currently active shard level channels matching a <pattern> (default: '*').
11) SHARDNUMSUB [<shardchannel> ...](分片订阅的数量)
12)     Return the number of subscribers for the specified shard level channel(s)
13) HELP
14)     Print this help.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值