码迷,mamicode.com
首页 > 系统相关 > 详细

cache 订单队列 - TP5

时间:2017-09-25 17:32:26      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:http   with   ==   status   实现   linu   cti   for   empty   

使用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

标签:http   with   ==   status   实现   linu   cti   for   empty   

原文地址:http://www.cnblogs.com/wqy415/p/7592602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!