发布(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.