redis(Remote Dictionary Server ),即遠程字典服務,是一個開源的使用ANSI?c語言編寫、支持網絡、可基于內存亦可持久化的日志型、Key-Value數據庫,并提供多種語言的API。
(學習視頻分享:redis視頻教程)
1.redis發布訂閱模式
Redis除了提供像list的這種的消息隊列模式,還提供了一組命令實現發布/訂閱模式。例如微博,公眾號等都是可以由此實現。
1.2 訂閱頻道
發布者需要將消息發送到一個地方,讓訂閱者可以訂閱消息,這個地方就是頻道(channel)。訂閱者可以訂閱一個或者多個頻道,所有訂閱了這個頻道的訂閱者都會受到這條消息。
開啟兩個客戶端進行測試
客戶端1?訂閱channel1 127.0.0.1:6379>?subscribe?channel1 Reading?messages...?(press?Ctrl-C?to?quit) 1)?"subscribe" 2)?"channel1" 3)?(integer)?1 客戶端2?發布一則消息 127.0.0.1:6379>?publish?channel1?test (integer)?1 客戶端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)?"test"
1.2 按規則訂閱
支持?和*占位符。?代表一個字符,*代表 0 個或者多個字符。
啟動四個redis-cli,一個作為消息的發布者,另外三個作為訂閱者。
訂閱者1:訂閱體育相關
psubscribe?*sport
訂閱者2:訂閱新聞相關
psubscribe?news*
訂閱者3:訂閱天氣相關
psubscribe?new?weather*
發布者:
publish?news-sport?Kobe publish?news-music?jaychou publish?news-weather?rain
此時訂閱者1將會收到Kobe,訂閱者2將會收到全部信息,訂閱者3將會收到rain。
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END