PHP解决抢购等阻塞式高并发redis处理思路

//各种验证之后

                $watchkey = "product_stock_95";
                $redis->redis->watch($watchkey);
                $redisstock = $redis->get($watchkey);
                if($redisstock!==false){
                    $stocknum = intval($redisstock);
                }else{
                    $stocknum = 0;
                }
                
                if($stocknum<=0) {
                    die(json_encode(array('success'=>false,'flag'=>0)));
                }
            do {
                    $newstock = $stocknum-$buynum;//库存-购买数量
                    $transactionResult = $redis->redis->multi()->set($watchkey,$newstock)->exec();//设置的库存
                    if($transactionResult){
                        //成功 业务处理


                    }else{
          
$redis->redis->watch($watchkey); $redisstock = $redis->get($watchkey); if($redisstock!==false){ $stocknum = intval($redisstock); }else{ $stocknum = 0; } if($stocknum<=0) { die(json_encode(array('success'=>false,'flag'=>0))); } } } while (!$transactionResult);

 

posted @ 2019-08-09 09:41  双木君  Views(616)  Comments(0Edit  收藏  举报