1. 发布与订阅频道

消息发布与订阅像收音机与广播台的关系

1.1. publish channel message 发布频道

语法:publish channel message

作用:发布频道消息

返回值:订阅数(类似粉丝数)

例:

127.0.0.1:6379> publish news 'today is sunday'

(integer) 0

1.2. subscribe channel [channel ...] 订阅频道

语法:subscribe channel [channel ...]

作用:订阅频道

例:

窗口1:

127.0.0.1:6379> publish news 'today is sunday'

(integer) 0

窗口2:

127.0.0.1:6379> subscribe news

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "news"

3) (integer) 1

此时看不到窗口1已经发过的消息,需要再发新消息才能看到

窗口1:(再发)

127.0.0.1:6379> publish news 'still sunday'

(integer) 1

窗口2:(收到)

1) "message"

2) "news"

3) "still sunday"

如果再开几个客户端,同样也能收到消息

1.3. psubscribe pattern [pattern ...] 订阅频道

语法:psubscribe pattern [pattern ...]

作用:可以通过匹配模式来一次性订阅多个频道

例:

127.0.0.1:6379> psubscribe news*

Reading messages... (press Ctrl-C to quit)

1) "psubscribe"

2) "news*"

3) (integer) 1

05-02 23:44