redis 订阅与发布

2018-06-28 02:00:04   php分享记录

  订阅,发布  

订阅

  1. <?php
  2. $redis = new Redis();
  3. $redis->pconnect("127.0.0.1",6379);
  4. $redis->auth("243356");
  5. $redis->setOption(Redis::OPT_READ_TIMEOUT,-1);
  6. $redis->subscribe(array('news'),'callback');
  7. function callback($instance,$channelName,$message){
  8. echo $channelName,"==>",json_decode($message)->msg,PHP_EOL;
  9. }

发布

  1. <?php
  2. $redis = new Redis();
  3. $redis->connect("127.0.0.1",6379);
  4. $redis->auth("243356");
  5. $redis->publish('news',json_encode(array('news','msg'=>"Hello world")));