<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # 進階篇九 使用Redis類庫處理一般的搶購(秒殺)活動示例 ### 創建搶購活動Redis類庫文件 ~~~ <?php /** * Created by PhpStorm. * User: Mikkle * QQ:776329498 * Date: 2017/6/28 * Time: 8:45 */ namespace app\base\service; use mikkle\tp_redis\RedisHashInfoBase; use think\Exception; class ScheduleDetail extends RedisHashInfoBase { protected $table="gopar_schedule_detail"; //數據表的 protected $pk = "id"; //數據表的主鍵 public function _initialize() { //判斷數據存在 并設置檢查周期10分鐘 if (!$this->checkLock("dataExists") && !$this->checkTableDataExists()){ throw new Exception("相關產品數據不存在"); }else{ //設置檢查鎖10分鐘 $this->setLock("dataExists",600); } //如果數據不存在 初始化讀取數據 if (!$this->checkExists()){ $this->initTableData(); } } public function getScheduleCenter() { return Schedule::instance( $this->getInfoFieldValue("schedule_id")); } public function __destruct() { //設置15天自動回收redis $this->setExpire((int)$this->getScheduleCenter()->getInfoFieldValue("end_time")+3600*24*15); } } ~~~ ## 在服務層或者控制器處理搶購邏輯 >[info] 在處理邏輯中 可以使用隨機去除了一部分的購買請求 以保證搶購活動平穩完成 > 當然也可以同時在前端請求中示例類似方法過濾 ~~~ public function index($data=["user_id"=>1,"ticket_detail_id"=>1,"buy_num"=>1]){ try { //檢測數據存在 if (!$this->checkArrayValueEmpty($data,["user_id","ticket_detail_id","buy_num"])){ throw new Exception($this->error); } $user_id= $data["user_id"] ; //用戶Id $ticket_detail_id = $data["ticket_detail_id"] ; //產品Id $buy_num = $data["buy_num"] ; //購買數量 $infoCenter= ScheduleDetail::instance( $ticket_detail_id ); $scheduleDetailInfo =$infoCenter->getInfoList(); //修改數據庫后 需要運行initTableData()方法重新初始化 推薦寫到Hook里 // $infoCenter->initTableData(); if ( $infoCenter->getInfoFieldValue( "hot_schedule")){ //熱門搶購隨機過濾隨機過濾 if (!in_array(rand(100, 200) % 11, [1, 3, 5, 7, 9])) { throw new Exception("搶票人數眾多 ,你被擠出搶購隊伍,還有余票,請重新再搶"); }; } // 這里判斷 購買數量和銷售日期 不符合就 throw new Exception if (!true){ throw new Exception("這里寫不符合原因"); } if (((int)$infoCenter->getInfoFieldValue("{$user_id}_num")+$buy_num)>$scheduleDetailInfo["limit_num"] ){ throw new Exception("你超過最大購買數量"); } if ($infoCenter->setInfoFieldIncre("pay_num",$buy_num) >$scheduleDetailInfo["limit_num"] ){ // $infoCenter->setInfoFieldIncre("pay_num", -$buy_num); throw new Exception("對不起,票已經賣光了!"); } //這里寫主邏輯 啟用事務功能創建訂單 //事務參見下節源碼 //升級已銷售數量 $infoCenter->updateTableData(["pay_num"]); //在這里推薦埋鉤子 處理訂單完成的后續事情 //返回結果 } catch (Exception $e) { Log::error($e->getMessage()); return ShowCode::jsonCodeWithoutData(1008, $e->getMessage()); } } } ~~~ >[danger] 可以參照定時隊列判斷訂單是否處理完成 校準剩余庫存 高階篇六 定時執行隊列類庫以及使用方法 http://www.hmoore.net/mikkle/thinkphp5_study/566146 >[info] 加QQ群 321449759 有完整版擴展類庫下載! >![](https://box.kancloud.cn/3499008a08e64306c68873288092a057_286x340.png) >
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看