cache 订单队列 - TP5

Posted wqy的笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cache 订单队列 - TP5相关的知识,希望对你有一定的参考价值。

使用cache实现一个简单粗糙的订单推送队列 

 

Linux  定时任务 

* * * * * /usr/bin/curl  http://tc.m.com/test.php

/**
 * User: [一秋]
 * Date: 2017/9/18
 * Time: 上午8:56
 * Desc: 成功来源于点滴
 */

namespace app\api\service;

//订单队列
use app\api\model\Order;

class OrderQueue
{
    private $order_queue;
    private $key;
    private $expire_time = 60;

    public $orderid;


    public function __construct()
    {
        $this->key = ‘your key value‘;
        $this->order_queue = $this->queryNotReceiveOrder();
    }

    //查询未接订单
    private function queryNotReceiveOrder()
    {
        $data = cache($this->key);

        if (!$data) {
            $lists = Order::with(‘receiveOrder‘)
                ->field(‘orderid‘)
                ->order(‘ctime desc‘)
                ->where(‘orderstatus‘, ‘=‘, 2)->page(1, 10)
                ->select();

            $data = [];
            foreach ($lists as $key => $val) {
                if ($val->receive_order == null) {
                    $data[] = $val->orderid;
                }
            }

            if (!empty($data)) {

                cache($this->key, $data, $this->expire_time);

            }

        }

        return $data;
    }

    //有新的订单 添加到队列中
    public function pushToQueue()
    {

        if (!$this->orderid) {
           return true;
        }

        if ($this->order_queue) {

            array_unshift($this->order_queue, $this->orderid);

        } else {
            $this->order_queue = ["$this->orderid"];
        }
        $this->order_queue = $this->array_unique();

        cache($this->key, $this->order_queue, $this->expire_time);

        return true;


    }

    //从订单队列中拉取数据 推送
    public function pullFromQueue()
    {

        if ($this->order_queue) {
            //提取第一个数据
//            $first_orderid = array_shift($this->order_queue);
//            Order::placeOrderGetuiList($first_orderid);
            //推送完毕 存放到数组最后
//            array_push($this->order_queue, $first_orderid);

            foreach ($this->order_queue as $key=>$val){
                Order::placeOrderGetuiList($val);//推送订单。
                sleep(10);
            }

//            cache($this->key) ? cache($this->key, null) : true;

        }
        return true;
    }

    //从订单队列中 移除
    public function clearOneOrder(){

        if($this->order_queue){
            $key = array_search($this->orderid,$this->order_queue);
            if(!$key && $key != 0){
                return true;
            }
            unset($this->order_queue[$key]);
            cache($this->key,$this->order_queue,$this->expire_time);
        }

        return true;

    }


    //数组去重
    private function array_unique(){
        return array_unique($this->order_queue);
    }

    public function pushToList($order_arr){

        if(!is_array($order_arr)){
            return true;
        }
        foreach ($order_arr as $key=>$val){
            $this->orderid = $val;
            $this->pushToQueue();
        }
        return true;
    }

}

 

以上是关于cache 订单队列 - TP5的主要内容,如果未能解决你的问题,请参考以下文章

tp5.0清除缓冲

tp5.0 结合 Redis Cache缓存风暴

TP5开启缓存

[李景山php]每天TP5-20161222|thinkphp5-Cache.php

TP5 redis 延迟队列

支付宝APP支付 统一下单 php服务端 tp5