redisケース--商品オークション
13002 ワード
コード説明:小さなプログラムで商品オークションを行い、複数人で奪い取り、ゼロ秒で奪い取り、redis(物事)で奪い取り価格の正確性を確認し、何も言わずにコードを添付!
/**
*
*/
public function plusPrice(){
$member_id = $this->userInfo['member_id']>0 ? $this->userInfo['member_id'] : 0;
$mobile = $this->userInfo['member_name']>0 ? $this->userInfo['member_name'] : 0;
$lb_id = input('lb_id',0);
$every_hand = input('price',0); //
$goods_id = input('goods_id',0);
$nickname = input('nickname','');
$avatar = input('avatar','');
$msg_type = input('msg_type','morker');
$this->redis->set('msgType',$msg_type);
//
if($lb_id<=0)
{
$this->returnmsg('2055', ' ID ');
}
if($goods_id<=0)
{
$this->returnmsg('2055', ' ID ');
}
if($member_id<=0)
{
$this->returnmsg('2055', ' ID ');
}
// redis
$thing_key = 'wxGoodsPlusPrice';
$this->redis->setex($thing_key, 60);
// keyTest
$this->redis->watch(array($thing_key));
$plus_max_price_key = 'plusMaxPrices:'.$lb_id.':'.$goods_id;
$iag_key = 'setAuctionGoods:'.$lb_id;
$iag_g_info_json = $this->redis->get($iag_key);
$iag_g_info = \json_decode($iag_g_info_json,true);
$plus_max_prices = $this->redis->lRange($plus_max_price_key,0,-1);
$max_price = max($plus_max_prices)>0 ? max($plus_max_prices) : $iag_g_info['starting_price']; // ,
$price = $max_price + $every_hand;
//
$this->redis->multi();
$this->redis->incr($thing_key);
$this->redis->lPush($plus_max_price_key,$price);
//
$result = $this->redis->exec();
// , ,
// ,
if($result){
$auction_goods_id_key = 'auctionGoodsId:'.$lb_id;
$auction_goods_id = $this->redis->get($auction_goods_id_key);
if(empty($auction_goods_id)){
$this->redis->lPop($plus_max_price_key);
$this->returnmsg('2055', ' ');
}
//
if($price<=$max_price){
$this->redis->lPop($plus_max_price_key);
$this->returnmsg('2055', ' ');
}
//
$max_price_uinfo_key = 'maxPriceUinfoKey:'.$lb_id.'-'.$goods_id;
$old_max_price_uinfo_json = $this->redis->get($max_price_uinfo_key);
$old_max_price_uinfo = json_decode($old_max_price_uinfo_json,true);
if($old_max_price_uinfo['user_id'] == $member_id){
$this->redis->lPop($plus_max_price_key);
$this->returnmsg('2055', ' ');
}
//
$unique_price_key = 'unique_price_key:'.$lb_id.':'.$goods_id;
$price_arr = $this->redis->lRange($unique_price_key,0,-1);
if(!in_array($price,$price_arr)){
$this->redis->lPush($unique_price_key,$price);
$max_price_uinfo = array(
'user_id'=>$member_id,
'mobile'=>$mobile,
'max_price'=>$price,
'goods_id'=>$goods_id,
'lb_id'=>$lb_id,
'nickname'=>$nickname,
'new_avatar'=>$avatar
);
$this->redis->set($max_price_uinfo_key,\json_encode($max_price_uinfo,true));
$plus_max_price_mtime_key = 'plusMaxMtimePrices:'.$lb_id.':'.$goods_id;
$this->redis->lPush($plus_max_price_mtime_key,$member_id.'--'.$price.'--'.$max_price.'--'.microtime(1));
$mq_plus_price_consumption_key = 'mq_plus_price_consumption';
$this->redis->lPush($mq_plus_price_consumption_key,\json_encode($max_price_uinfo,true));
// QueueClient::push(new LiveImSendMsgProcessQueue(), ['reset_name' => ['live_im_send_msg'],'lb_id'=>$lb_id,'user_id'=>$member_id,'price'=>$price]);
}
}
$this->returnmsg('200', 'success');
}