redis案例 -- 商品拍卖
代码说明:在小程序上进行商品拍卖,多人抢拍,零秒抢拍,用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‘); }
相关推荐
王道革 2020-11-25
wangdonghello 2020-11-03
Langeldep 2020-11-16
chenhualong0 2020-11-16
聚合室 2020-11-16
koushr 2020-11-12
MRFENGG 2020-11-11
guoyanga 2020-11-10
fackyou00 2020-11-10
Orangesss 2020-11-03
dongCSDN 2020-10-31
rainandtear 2020-10-30
Quietboy 2020-10-30
liuyulong 2020-10-29
fansili 2020-10-29
温攀峰 2020-10-23
jackbon 2020-10-19
kaixinfelix 2020-10-04