使用redis创建一个队列
创建一个Queue类,使其拥有push,pop,getSize属性。符合队列的先进先出原则
<?php
/**
* Created by PhpStorm.
* 使用redis的list类型创建一个消息队列
* User: liwenqiang
* Date: 2018/5/17
* Time: 10:58
*/
namespace App\Models\RedisList;
use Predis\Client;
class Queue
{
private $queueName;
private $queueKey;
private $client;
private $timeout;
public function __construct($queueName)
{
$this->client = new Client();
$this->queueName = $queueName;
$this->queueKey = "queue:".$queueName;
$this->timeout = 0;
}
public function getSize()
{
return $this->client->llen($this->queueKey);
}
public function push($element)
{
$this->client->lpush($this->queueKey,$element);
}
public function pop()
{
return $this->client->brpop([$this->queueKey],$this->timeout);
}
}