Redis 發佈訂閱(pub/sub)是一種消息通訊模式:發送者(pub)發送消息,訂閱者(sub)接收消息。正則表達式
Redis 客戶端能夠訂閱任意數量的頻道。ui
127.0.0.1:6379> subscribe channel:1 Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel:1" 3) (integer) 1
127.0.0.1:6379> subscribe channel:1 channel:2 Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel:1" 3) (integer) 1 1) "subscribe" 2) "channel:2" 3) (integer) 2
127.0.0.1:6379> publish channel:1 hello (integer) 2 # 訂閱者個數,說明有兩個訂閱者訂閱該頻道。
Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel:1" 3) (integer) 1 1) "message" 2) "channel:1" 3) "hello"
Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel:1" 3) (integer) 1 1) "subscribe" 2) "channel:2" 3) (integer) 2 1) "message" 2) "channel:1" 3) "hello"
127.0.0.1:6379> publish channel:2 world (integer) 1 # 訂閱者個數,說明有1個訂閱者訂閱該頻道。
Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel:1" 3) (integer) 1 1) "message" 2) "channel:1" 3) "hello"
Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel:1" 3) (integer) 1 1) "subscribe" 2) "channel:2" 3) (integer) 2 1) "message" 2) "channel:1" 3) "hello" 1) "message" 2) "channel:2" 3) "world"