zoukankan      html  css  js  c++  java
  • Redis发布订阅使用方法

    Redis发布订阅

    发布订阅模式中发布消息的为publisher即发布者,接收消息的为subscriber即订阅者。在Redis中,所有的消息通过channel即频道进行发布,一个发布者可以向多个channel发布消息,一个订阅者也可以订阅多个channel。Redis不对消息进行持久化,如果消息发布时订阅者还没有进行订阅,则不会再收到此消息。

    发布订阅命令

    命令 格式 说明
    PUBLISH PUBLISH channel message 发布message到指定的channel
    SUBSCRIBE SUBSCRIBE channel [channel ...] 订阅1个或多个指定的channel
    UNSUBSCRIBE UNSUBSCRIBE [channel [channel ...]] 取消订阅1个或多个指定的channel,如果不指定channel退订所有通过SUBSCRIBE订阅的channel
    PSUBSCRIBE PSUBSCRIBE pattern [pattern ...] 根据匹配模式订阅channel
    PUNSUBSCRIBE PUNSUBSCRIBE [pattern [pattern ...]] 根据匹配模式取消订阅channel,如果不指定匹配模式退订所有通过PSUBSCRIBE订阅的channel

    subscribe不能重复订阅同一个channel,而psubscribe按照匹配模式订阅时有可能会多次订阅同一个channel。如果psubscribe多次订阅了同一个channel,发布者使用publish发布消息到此channel后,订阅者会多次收到此消息。

    unsubscribe只能退订subscribe订阅的channel,punsubscribe只能退订psubscribe订阅的channel。

    使用方法

    1、订阅指定channel

    192.168.1.100:6379> subscribe testchan  
    Reading messages... (press Ctrl-C to quit)  
    1) "subscribe" #订阅成功  
    2) "testchan" #订阅的channel名称  
    3) (integer) 1 #已订阅的channel个数  
       
    1) "message" #收到消息  
    2) "testchan" #消息来源channel  
    3) "hello" #消息内容  
       
    192.168.1.100:6379> publish testchan hello  
    (integer) 1 #收到消息的订阅者的数量
    

      

    2、订阅指定匹配模式的channel

    192.168.1.100:6379> psubscribe test*  
    Reading messages... (press Ctrl-C to quit)  
    1) "psubscribe" #订阅成功  
    2) "test*" #订阅的channel匹配模式  
    3) (integer) 1 #已订阅的channel匹配模式个数  
       
    1) "pmessage" #收到消息  
    2) "test*" #匹配的订阅模式  
    3) "testchan" #消息来源channel  
    4) "hello" #消息内容  
       
    192.168.1.100:6379> publish testchan hello  
    (integer) 1 #收到消息的订阅者的数量  
    

      

    原文地址:http://caiguoqing.org/post/110

  • 相关阅读:
    用AVIFile函数制做AVI文件基本步骤
    RHEL5下源码安装Mysql
    RHEL 6.2/i686配置使用CentOS YUM源
    教你选择最稳定的 MySQL 版本
    RHEL 6.2/i686桌面版解决风扇狂转的问题 安装官方闭源ATI显卡驱动
    Ubuntu 11.10下解决JUK播放MP3乱码的方法
    Ubuntu 10.04下SVN+Apache安装、配置与使用
    Ubuntu 11.10安装(卸载)ATI闭源驱动导致黑屏进不了系统的解决办法
    ubuntu 11.10下创建eclipse桌面快捷方式
    Ubuntu 11.10与Windows双系统的硬盘安装方法
  • 原文地址:https://www.cnblogs.com/chengxuzhixin/p/6393298.html
Copyright © 2011-2022 走看看