簡単なdemoプレゼンテーションswoole_timer_tick消費redisのキュー

2522 ワード

先行生産対象

connect('127.0.0.1',6379);

$redis->auth('tb');

for($i=0;$i<10;$i++){
  $res[]=$redis->lpush('lists',$i);
}

var_dump($res);

プロダクション

root@tb:/usr/share/nginx/swoole_redis# php producter.class.php 
array(10) {
  [0]=>
  int(71)
  [1]=>
  int(72)
  [2]=>
  int(73)
  [3]=>
  int(74)
  [4]=>
  int(75)
  [5]=>
  int(76)
  [6]=>
  int(77)
  [7]=>
  int(78)
  [8]=>
  int(79)
  [9]=>
  int(80)
}

cliでリストの長さを見る

127.0.0.1:6379> llen lists
(integer) 80
127.0.0.1:6379> 

消費対象

connect(self::$host,self::$port);
        $redis->auth=self::$password;
        self::$_instance=$this;
        self::$redisObj=$redis;
        return self::$_instance;
  }

  public static function instance(array $param){
    self::$host=$param['host'];
    self::$port=$param['port'];
    self::$password=$param['password'];
    self::$list=$param['list'];
    if(!empty(self::$_instance)){
      return self::$_instance;
    }else{
      return new self();
    }
  }

  public function getMsg(){
    return self::$redisObj->rPop(self::$list);
  }
}

swoole_timer_tick('1000',function(){
  $params['host']='127.0.0.1';
  $params['port']=6379;
  $params['password']='tb';
  $params['list']='lists';//KEY  lists
  $s=Customer::instance($params);
  var_dump( $s->getMsg());
});

消費、

root@tb:/usr/share/nginx/swoole_redis# php  customer.php 
string(1) "0"
string(1) "1"
string(1) "2"
string(1) "3"
string(1) "4"
string(1) "5"
string(1) "6"
string(1) "7"
string(1) "8"
string(1) "9"
string(1) "0"
string(1) "1"
string(1) "2"
string(1) "3"
string(1) "4"
^C
root@tb:/usr/share/nginx/swoole_redis# 

15個消費して、残り65個

127.0.0.1:6379> llen lists
(integer) 80
127.0.0.1:6379> llen lists
(integer) 65
127.0.0.1:6379>