简单demo 演示swoole_timer_tick 消费redis 的队列

先上生产对象

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看看list长度

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> 

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