Redis学习笔记四(发布订阅)
1. 发布订阅
- 发布订阅是一种消息通信模式,发布者发送消息,订阅者接收消息
- redis客户端可以订阅任意数量的频道(一个发布者可以有多个发布频道)
2. 实现
- 订阅频道
subscribe <channel>
127.0.0.1:6379> subscribe channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
- 发布数据到频道
publish <channel> <数据>
127.0.0.1:6379> publish channel1 hello
(integer) 1
- 查看
127.0.0.1:6379> subscribe channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
1) "message"
2) "channel1"
3) "hello"