1、創建搶購活動Redis類庫文件
<?php /** * Created by PhpStorm. */ 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); } }
2、在服務層或者控制器處理搶購邏輯
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()); } } }
3.定時隊列判斷訂單是否處理完成 校准剩余庫存
<?php /** * Created by PhpStorm. */ namespace mikkle\tp_worker; use mikkle\tp_master\Exception; use mikkle\tp_master\Log; /** * title 定時隊列類 * Class TimingWorkerBase * @package mikkle\tp_worker * 創建定時隊列類並繼承使用方法 * class Test extends TimingWorkerBase * { * protected function runHandle($data) * { * Log::notice( "測試".RandNumCenter::getTimeString() ); * } * } * * 添加方法定時隊列方法 * \app\worker\Test::add(["name"=>"mikkle",],30); */ abstract class TimingWorkerBase extends WorkerBase { protected $listName; protected $listData; protected $listNum; protected $lockName; public function _initialize($options = []) { $this->listData = "{$this->listName}_data"; $this->listNum = "{$this->listName}_num"; } /** * * 快速定時任務 * * 當命令行未運行 直接執行 * description add * @param $data * @param $runTime * @param array $options * @param string $handleName * @return bool */ static public function add($data, $runTime = 0, $handleName = "run", $options = []) { try { $data = json_encode($data); $instance = static::instance($options); switch (true) { case (self::checkCommandRun()): $time = $instance->getRunTime($runTime); $num = $instance->redis()->incre($instance->listNum); Log::notice("添加了 $num 號定時任務"); $instance->redis()->zAdd($instance->listName, [$time => $num]); $instance->redis()->hSet($instance->listData, $num, $data); Log::notice("Timing Command service start work!!"); $instance->runWorker($handleName); break; default: Log::notice("Timing Command service No away!!"); $instance->runHandle($data); } return true; } catch (Exception $e) { Log::error($e->getMessage()); return false; } } /** * 命令行執行的方法 */ static public function run() { try { $i = 0; $instance = static::instance(); //讀取並刪除定時任務 $workList = $instance->redis()->zRangByScore($instance->listName, 0, time()); $instance->redis()->zDelete($instance->listName, $workList); //剩余任務數 $re = $instance->redis()->zCard($instance->listName); if ( $workList ){ foreach ($workList as $num) { try { $redisData = $instance->redis()->hGet($instance->listData, $num); if ($redisData) { $data = json_decode($redisData, true); $result = $instance->runHandle($data); Log::notice("執行{$num}編號任務"); if ($instance->saveLog) { $instance->saveRunLog($result, $data); } $instance->redis()->hDel($instance->listData, $num); } } catch (Exception $e) { Log::error($e->getMessage()); $instance->redis()->zAdd($instance->listData, [(time() + 300) => $num]); } $i++; sleep(1); } } if ( $re=== 0) { $instance->clearWorker(); } echo "執行了{$i}次任務,剩余未執行任務[{$re}]項" . PHP_EOL; Log::notice("執行了{$i}次任務,剩余未執行任務[{$re}]項"); } catch (Exception $e) { //Log::error($e); Log::error($e->getMessage()); echo($e->getMessage()); } } protected function getRunTime($time = 0) { $now = time(); switch (true) { case ($time == 0): return $now; break; case (is_int($time) && 30 * 3600 * 24 > $time): return $now + $time; break; case (is_int($time) && $now < $time): return $time; break; default: return $now + (int)$time; } } }