zoukankan      html  css  js  c++  java
  • cache 订单队列

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

    Linux  定时任务 

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

    /**
     * User: [一秋]
     * Date: 2017/9/18
     * Time: 上午8:56
     * Desc: 成功来源于点滴
     */
    
    namespace appapiservice;
    
    //订单队列
    use appapimodelOrder;
    
    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;
        }
    
    }
  • 相关阅读:
    vue项目,百度地图api高亮选取区域,高亮某个地区,行政区域等
    vue 项目, 通知子组件更新,父组件中每次点击按钮重新加载子组件,(重新生成dom 元素)
    洛谷 P1003 铺地毯
    Codeforces Round #582 (Div. 3)
    安科 OJ 1190 连接电脑 (并查集)
    2018年牛客多校寒假 第四场 F (call to your teacher) (图的连通性)
    牛客小白月赛16 A 小石的签到题 ( 博弈)
    牛客小白月赛16 E 小雨的矩阵 ( 暴搜)
    安科 OJ 1054 排队买票 (递归,排列组合)
    牛客小白月赛15 C 表单 ( map 使用)
  • 原文地址:https://www.cnblogs.com/wqy415/p/7592602.html
Copyright © 2011-2022 走看看