用PHP实现一个简单的环形队列

缺少一个好的计时器. 但是功能能正常运行. 替换成一个好的计时器。状态可以保存在DB中.

/**
 * 环型队列.
 * 创建一个环形为3000个元素.
 * 作cron,其中每秒走一个位置.
 */

/**
 * Class Task
 */
class Task
{

    private $TaskId;
    private $Time;
    private $CallBack;
    private $Run;

    public function __construct($TaskId = null, $Time = null, $CallBack = null)
    {
        $this->Run = FALSE;
        $this->SetId($TaskId);
        $this->SetTime($Time);
        $this->SetCallback($CallBack);
    }

    public function SetId($TaskId)
    {
        $this->TaskId = $TaskId;
    }

    public function GetId()
    {
        return $this->TaskId;
    }

    public function SetTime($Time)
    {
        $this->Time = $Time;
    }

    public function GetTime()
    {
        return $this->Time;
    }

    public function SetCallBack($CallBack)
    {
        $this->CallBack = $CallBack;
    }

    public function GetCallBack()
    {
        return $this->CallBack;
    }

    public function Run()
    {
        if (!$this->Run && is_callable($this->GetCallBack())) {
            $this->Run = TRUE;
            $this->GetCallBack();
        }
    }
}

/**
 * Class Mqueue
 *
 * 3000的环形队列.
 *
 */
class Mqueue
{
    private $Queue;
    private $Tasks;
    private $CurrentTask;
    private $QueueLenth = 3000;
    private $CurrentIndex; // 当前为在哪个格子里面.
    private $CurrentNum;  // 当前在第几圈.

    /**
     * Add task.
     *
     */
    public function AddTask(Task $task)
    {
        $this->Tasks[$task->GetId()] = $task;
        $this->CurrentTask = $task->GetId();
        $this->SetQueue($task);
    }

    /**
     * 将任务添加到环中.
     *
     * @param $task
     */
    private function SetQueue($task)
    {
        $postion = $this->GetPosition($task);
        $this->Queue[$postion[1]][$postion[0]][$task->GetId()] = $task;
    }

    /**
     * 计算任务在当前哪个环中.
     *
     * @param $task
     *
     * @return array
     */
    private function GetPosition($task)
    {
        $time = strtotime($task->GetTime()) - time();

        return [
          $time % $this->QueueLenth,
          $this->CurrentNum + floor($time / $this->QueueLenth),
        ];
    }

    private function Timer()
    {
        $this->CurrentIndex = 0;
        $this->CurrentNum = 0;
        $this->Ring();
    }

    public function GetQueue() {
        return $this->Queue;
    }

    /**
     * @TODO Good timer.
     */
    private function Ring() {
        // 每秒跑一格.
        // 到尾了跑第二环.
        for($i = 0; $i <= $this->QueueLenth; $i++) {
            $this->CurrentIndex++;
            if(isset($this->Queue[$this->CurrentNum][$this->CurrentIndex])) {
                $this->Queue[$this->CurrentNum][$this->CurrentIndex]->Run();
            }
            sleep(1000);

            if($this->CurrentIndex <= $this->QueueLenth) {
                $this->CurrentNum++;
                $this->Ring();
            }

        }
    }
}

$task = new Task('thisismyfirsttask', '2017-3-21', function () {
        echo 'hello world';
    }
);

$mq = New Mqueue();
$mq->AddTask($task);
print_r($mq->GetQueue());

1 thought on “用PHP实现一个简单的环形队列”

Leave a Reply

Your email address will not be published. Required fields are marked *