這篇文章主要介紹TP5如何使用Redis處理電商秒殺,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

TP5使用Redis處理電商秒殺
1、首先在TP5中創(chuàng)建搶購活動所需要的Redis類庫文件,代碼如下:
<php
namespace app\base\service;
use mikkle\tp_redis\RedisHashInfoBase;
use think\Exception;
class ScheduleDetail extends RedisHashInfoBase
{
protected $table="gopar_schedule_detail"; //數(shù)據(jù)表的
protected $pk = "id"; //數(shù)據(jù)表的主鍵
public function _initialize()
{
//判斷數(shù)據(jù)存在 并設(shè)置檢查周期10分鐘
if (!$this->checkLock("dataExists") && !$this->checkTableDataExists()){
throw new Exception("相關(guān)產(chǎn)品數(shù)據(jù)不存在");
}else{
//設(shè)置檢查鎖10分鐘
$this->setLock("dataExists",600);
}
//如果數(shù)據(jù)不存在 初始化讀取數(shù)據(jù)
if (!$this->checkExists()){
$this->initTableData();
}
}
public function getScheduleCenter()
{
return Schedule::instance( $this->getInfoFieldValue("schedule_id"));
}
public function __destruct()
{
//設(shè)置15天自動回收redis
$this->setExpire((int);
$this>getScheduleCenter()->getInfoFieldValue("end_time")+3600*24*15);
}
}2、在服務(wù)層或者控制器處理搶購邏輯,代碼如下:
public function index($data=["user_id"=>1,"ticket_detail_id"=>1,"buy_num"=>1]){
try {
//檢測數(shù)據(jù)存在
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"] ; //產(chǎn)品Id
$buy_num = $data["buy_num"] ; //購買數(shù)量
$infoCenter= ScheduleDetail::instance( $ticket_detail_id );
$scheduleDetailInfo =$infoCenter->getInfoList();
//修改數(shù)據(jù)庫后 需要運行initTableData()方法重新初始化 推薦寫到Hook里
// $infoCenter->initTableData();
if ( $infoCenter->getInfoFieldValue( "hot_schedule")){
//熱門搶購隨機(jī)過濾隨機(jī)過濾
if (!in_array(rand(100, 200) % 11, [1, 3, 5, 7, 9])) {
throw new Exception("搶票人數(shù)眾多 ,你被擠出搶購隊伍,還有余票,請重新再搶");
};
}
// 這里判斷 購買數(shù)量和銷售日期 不符合就 throw new Exception
if (!true){
throw new Exception("這里寫不符合原因");
}
if (((int)$infoCenter->getInfoFieldValue("{$user_id}_num")+$buy_num)>$scheduleDetailInfo["limit_num"] ){
throw new Exception("你超過較大購買數(shù)量");
}
if ($infoCenter->setInfoFieldIncre("pay_num",$buy_num) >$scheduleDetailInfo["limit_num"] ){
//
$infoCenter->setInfoFieldIncre("pay_num", -$buy_num);
throw new Exception("對不起,票已經(jīng)賣光了!");
}
//這里寫主邏輯 啟用事務(wù)功能創(chuàng)建訂單
//事務(wù)參見下節(jié)源碼
//升級已銷售數(shù)量
$infoCenter->updateTableData(["pay_num"]);
//在這里推薦埋鉤子 處理訂單完成的后續(xù)事情
//返回結(jié)果
} catch (Exception $e) {
Log::error($e->getMessage());
return ShowCode::jsonCodeWithoutData(1008, $e->getMessage());
}
}
}在處理邏輯中 可以使用隨機(jī)去除了一部分的購買請求 以保證搶購活動平穩(wěn)完成
當(dāng)然也可以同時在前端請求中示例類似方法過濾
可以參照定時隊列判斷訂單是否處理完成 校準(zhǔn)剩余庫存
以上是“TP5如何使用Redis處理電商秒殺”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:TP5如何使用Redis處理電商秒殺-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://www.chinadenli.net/article32/idepc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、企業(yè)網(wǎng)站制作、云服務(wù)器、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容