redis----发布订阅

发布订阅

命令

  • 订阅
subscribe 频道名 [频道名 ...]

例如:创建一个频道名为animal_channel

127.0.0.1:6379> subscribe animal_channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "animal_channel"
3) (integer) 1
  • 发布消息
publish 频道 消息

例如:重新开一个终端,给animal_channel发送消息

127.0.0.1:6379> publish animal_channel 'panda is cute '
(integer) 1
127.0.0.1:6379> publish animal_channel 'I like panda'
(integer) 1

开启了订阅的anim_channel频道的终端接收到的数据

127.0.0.1:6379> subscribe animal_channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "animal_channel"
3) (integer) 1
1) "message"
2) "animal_channel"
3) "panda is cute "
1) "message"
2) "animal_channel"
3) "I like panda"
  • 取消订阅
  • 如果不写频道名,则表示取消所有频道
unsubscribe 频道名 [频道名 ...]

例如:取消已经订阅的animal_channel

127.0.0.1:6379> unsubscribe animal_channel
1) "unsubscribe"
2) "animal_channel"
3) (integer) 0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值