Redis 7.redis的发布订阅

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/hgx_suiyuesusu/article/details/85836160

Redis

@Author:hanguixian
@Email:[email protected]

七 Redis的发布订阅

1 是什么

2 使用

2.1 命令

  • PSUBSCRIBE:订阅一个或多个符合给定模式的频道
  • PUBLISH:将信息发送到指定的频道
  • PUBSUB:查看订阅与发布系统状态
  • PUNSUBSCRIBE:退订所有给定模式的频道
  • SUBSCRIBE:订阅给定的一个或多个频道的信息
  • UNSUBSCRIBE:指退订给定的频道

2.2 示例

  • 1 先订阅后发布后才能收到消息,可以一次性订阅多个,SUBSCRIBE c1 c2 c3
  • 2 消息发布,PUBLISH c2 hello-redis
  • 3 订阅多个,通配符*, PSUBSCRIBE new*``
  • 4 收取消息, PUBLISH new1 redis2015
##终端1##
127.0.0.1:6379> SUBSCRIBE c1 c2 c3
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "c1"
3) (integer) 1
1) "subscribe"
2) "c2"
3) (integer) 2
1) "subscribe"
2) "c3"
3) (integer) 3
1) "message"
2) "c2"
3) "hahaha"
1) "message"
2) "c2"
3) "hahahaggg"
##终端2##
127.0.0.1:6379> PUBLISH c2 hahaha
(integer) 1
127.0.0.1:6379> PUBLISH c2 hahahaggg
(integer) 1

猜你喜欢

转载自blog.csdn.net/hgx_suiyuesusu/article/details/85836160