Redis-订阅与发布

Redis-订阅与发布

本节讲一下redis的订阅与发布功能,但是不建议使用。毕竟术业有专攻,redis用于数据缓存,订阅发布还是给MQ去做。

##SUBSCRIBE订阅命令,可订阅多个主题
127.0.0.1:6379> SUBSCRIBE channel-1 channel-2 channel-3
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel-1"
3) (integer) 1
1) "subscribe"
2) "channel-2"
3) (integer) 2
1) "subscribe"
2) "channel-3"
3) (integer) 3

##PUBLISH发布命令
127.0.0.1:6379> PUBLISH channel-1 test
(integer) 1
##发布成功会在消费端收到消息
1) "message"
2) "channel-1"
3) "test"

##PSUBSCRIBE订阅命令,支持个模式以 * 作为匹配符
127.0.0.1:6379> PSUBSCRIBE channel*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "channel*"
3) (integer) 1

你可能感兴趣的:(redis,缓存)