redis:消息发布与订阅频道
1.?發(fā)布與訂閱頻道
消息發(fā)布與訂閱像收音機(jī)與廣播臺(tái)的關(guān)系
?
1.1.?publish channel message 發(fā)布頻道
語(yǔ)法:publish channel message
作用:發(fā)布頻道消息
返回值:訂閱數(shù)(類似粉絲數(shù))
例:
127.0.0.1:6379> publish news 'today is sunday'
(integer) 0
1.2.?subscribe channel [channel ...] 訂閱頻道
語(yǔ)法: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
此時(shí)看不到窗口1已經(jīng)發(fā)過(guò)的消息,需要再發(fā)新消息才能看到
?
窗口1:(再發(fā))
127.0.0.1:6379> publish news 'still sunday'
(integer) 1
?
窗口2:(收到)
1) "message"
2) "news"
3) "still sunday"
?
如果再開(kāi)幾個(gè)客戶端,同樣也能收到消息
1.3.?psubscribe pattern [pattern ...] 訂閱頻道
語(yǔ)法:psubscribe pattern [pattern ...]
作用:可以通過(guò)匹配模式來(lái)一次性訂閱多個(gè)頻道
例:
127.0.0.1:6379> psubscribe news*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "news*"
3) (integer) 1
轉(zhuǎn)載于:https://www.cnblogs.com/reyinever/p/10041269.html
總結(jié)
以上是生活随笔為你收集整理的redis:消息发布与订阅频道的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java IO总结
- 下一篇: php 栈、 出栈、入栈