Redis - SUBSCRIBE、PSUBSCRIBE、UNSUBSCRIBE、PUNSUBSCRIBE 訂閱功能介紹

SUBSCRIBE 訂閱

Subscribe 允許 client 端訂閱一個或多個 channels

> SUBSCRIBE channel [channel ...]

當 client 端 subscribe 某個 channels 之後,

除了subscribe相關指令(subscribe, psubscribe, unsubscribe, punsubscribe ),

一律不允許使用其他 commands

> SUBSCRIBE xxxchannel 
Reading messages...(press Ctrl-C to quit)
1) "subscribe"
2) "xxxchannel"
3) (integer) 1
1) "message"
2) "xxxchannel"
3) "a"

PSUBSCRIBE

使用 PSUBSCRIBE 訂閱一個或多個頻道時,可以透過一些模式,來訂閱

? : 表示任一字元

  • : 表示多個任意字元 [nnnn] : 表示在這其中任一字元

例如: H?llo 會包含 Hello, Hallo, Hbllo, Hcllo, ……. H*llo 會包含 Heeeeello, Hllo, Hxllo…… H[ae]llo 會包含 Hello, Hallo (但不包括 Hllo)

> PSUBSCRIBE xxxchannel
Reading messages...(press Ctrl-C to quit)
1) "psubscribe"
2) "xxxchannel"
3) (integer) 1

UNSUBSCRIBE

解除訂閱,可以指定要解除的channel,若沒有指定,則會解除全部已訂閱的channel

> UNSUBSCRIBE xxxchannel 
1) "unsubscribe"
2) "a"
3) (integer) 0

PUNSUBSCRIBE

可以解除所有 PSUBSCRIBE 的 channel

> PUNSUBSCRIBE xxxchannel
1) "punsubscribe"
2) "a"
3) (integer) 1