高并发抢购

1 <?php
 2 $conn=mysql_connect("localhost","big","123456"); 
 3 if(!$conn){ 
 4     echo "connect failed"; 
 5     exit; 
 6 } 
 7 mysql_select_db("big",$conn); 
 8 mysql_query("set names utf8");
 9  
10 $price=10;
11 $user_id=1;
12 $goods_id=1;
13 $sku_id=11;
14 $number=1;
15  
16 //生成唯一订单
17 function build_order_no(){
18   return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
19 }
20 //记录日志
21 function insertLog($event,$type=0){
22     global $conn;
23     $sql="insert into ih_log(event,type) 
24     values('$event','$type')"; 
25     mysql_query($sql,$conn); 
26 }
27  
28 //模拟下单操作
29 //库存是否大于0
30 $sql="select number from ih_store where goods_id='$goods_id' and sku_id='$sku_id'";//解锁 此时ih_store数据中goods_id='$goods_id' and sku_id='$sku_id' 的数据被锁住(注3),其它事务必须等待此次事务 提交后才能执行
31 $rs=mysql_query($sql,$conn);
32 $row=mysql_fetch_assoc($rs);
33 if($row['number']>0){//高并发下会导致超卖
34     $order_sn=build_order_no();
35     //生成订单 
36     $sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price) 
37     values('$order_sn','$user_id','$goods_id','$sku_id','$price')"; 
38     $order_rs=mysql_query($sql,$conn); 
39      
40     //库存减少
41     $sql="update ih_store set number=number-{$number} where sku_id='$sku_id'";
42     $store_rs=mysql_query($sql,$conn); 
43     if(mysql_affected_rows()){ 
44         insertLog('库存减少成功');
45     }else{ 
46         insertLog('库存减少失败');
47     } 
48 }else{
49     insertLog('库存不够');
50 }
复制代码

 

出现这种情况怎么办呢?来看几种优化方法:

优化方案1:将库存字段number字段设为unsigned,当库存为0时,因为字段不能为负数,将会返回false

复制代码
1 //库存减少
2 $sql="update ih_store set number=number-{$number} where sku_id='$sku_id' and number>0";
3 $store_rs=mysql_query($sql,$conn); 
4 if(mysql_affected_rows()){ 
5     insertLog('库存减少成功');
6 }
复制代码

优化方案2:使用MySQL的事务,锁住操作的行

复制代码
 1 <?php
 2 $conn=mysql_connect("localhost","big","123456"); 
 3 if(!$conn){ 
 4     echo "connect failed"; 
 5     exit; 
 6 } 
 7 mysql_select_db("big",$conn); 
 8 mysql_query("set names utf8");
 9  
10 $price=10;
11 $user_id=1;
12 $goods_id=1;
13 $sku_id=11;
14 $number=1;
15  
16 //生成唯一订单号
17 function build_order_no(){
18   return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
19 }
20 //记录日志
21 function insertLog($event,$type=0){
22     global $conn;
23     $sql="insert into ih_log(event,type) 
24     values('$event','$type')"; 
25     mysql_query($sql,$conn); 
26 }
27  
28 //模拟下单操作
29 //库存是否大于0
30 mysql_query("BEGIN");   //开始事务
31 $sql="select number from ih_store where goods_id='$goods_id' and sku_id='$sku_id' FOR UPDATE";//此时这条记录被锁住,其它事务必须等待此次事务提交后才能执行
32 $rs=mysql_query($sql,$conn);
33 $row=mysql_fetch_assoc($rs);
34 if($row['number']>0){
35     //生成订单 
36     $order_sn=build_order_no(); 
37     $sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price) 
38     values('$order_sn','$user_id','$goods_id','$sku_id','$price')"; 
39     $order_rs=mysql_query($sql,$conn); 
40      
41     //库存减少
42     $sql="update ih_store set number=number-{$number} where sku_id='$sku_id'";
43     $store_rs=mysql_query($sql,$conn); 
44     if(mysql_affected_rows()){ 
45         insertLog('库存减少成功');
46         mysql_query("COMMIT");//事务提交即解锁
47     }else{ 
48         insertLog('库存减少失败');
49     }
50 }else{
51     insertLog('库存不够');
52     mysql_query("ROLLBACK");
53 }
复制代码

优化方案3:使用非阻塞的文件排他锁

复制代码
 1 <?php
 2 $conn=mysql_connect("localhost","root","123456"); 
 3 if(!$conn){ 
 4     echo "connect failed"; 
 5     exit; 
 6 } 
 7 mysql_select_db("big-bak",$conn); 
 8 mysql_query("set names utf8");
 9  
10 $price=10;
11 $user_id=1;
12 $goods_id=1;
13 $sku_id=11;
14 $number=1;
15  
16 //生成唯一订单号
17 function build_order_no(){
18   return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
19 }
20 //记录日志
21 function insertLog($event,$type=0){
22     global $conn;
23     $sql="insert into ih_log(event,type) 
24     values('$event','$type')"; 
25     mysql_query($sql,$conn); 
26 }
27  
28 $fp = fopen("lock.txt", "w+");
29 if(!flock($fp,LOCK_EX | LOCK_NB)){
30     echo "系统繁忙,请稍后再试";
31     return;
32 }
33 //下单
34 $sql="select number from ih_store where goods_id='$goods_id' and sku_id='$sku_id'";
35 $rs=mysql_query($sql,$conn);
36 $row=mysql_fetch_assoc($rs);
37 if($row['number']>0){//库存是否大于0
38     //模拟下单操作 
39     $order_sn=build_order_no(); 
40     $sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price) 
41     values('$order_sn','$user_id','$goods_id','$sku_id','$price')"; 
42     $order_rs=mysql_query($sql,$conn); 
43      
44     //库存减少
45     $sql="update ih_store set number=number-{$number} where sku_id='$sku_id'";
46     $store_rs=mysql_query($sql,$conn); 
47     if(mysql_affected_rows()){ 
48         insertLog('库存减少成功');
49         flock($fp,LOCK_UN);//释放锁
50     }else{ 
51         insertLog('库存减少失败');
52     } 
53 }else{
54     insertLog('库存不够');
55 }
56 fclose($fp);
复制代码

优化方案4:使用redis队列,因为pop操作是原子的,即使有很多用户同时到达,也是依次执行,推荐使用(mysql事务在高并发下性能下降很厉害,文件锁的方式也是)

先将商品库存如队列

复制代码
 1 <?php
 2 $store=1000;
 3 $redis=new Redis();
 4 $result=$redis->connect('127.0.0.1',6379);
 5 $res=$redis->llen('goods_store');
 6 echo $res;
 7 $count=$store-$res;
 8 for($i=0;$i<$count;$i++){
 9     $redis->lpush('goods_store',1);
10 }
11 echo $redis->llen('goods_store');
复制代码

抢购、描述逻辑

复制代码
 1 <?php
 2 $conn=mysql_connect("localhost","big","123456"); 
 3 if(!$conn){ 
 4     echo "connect failed"; 
 5     exit; 
 6 } 
 7 mysql_select_db("big",$conn); 
 8 mysql_query("set names utf8");
 9  
10 $price=10;
11 $user_id=1;
12 $goods_id=1;
13 $sku_id=11;
14 $number=1;
15  
16 //生成唯一订单号
17 function build_order_no(){
18   return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
19 }
20 //记录日志
21 function insertLog($event,$type=0){
22     global $conn;
23     $sql="insert into ih_log(event,type) 
24     values('$event','$type')"; 
25     mysql_query($sql,$conn); 
26 }
27  
28 //模拟下单操作
29 //下单前判断redis队列库存量
30 $redis=new Redis();
31 $result=$redis->connect('127.0.0.1',6379);
32 $count=$redis->lpop('goods_store');
33 if(!$count){
34     insertLog('error:no store redis');
35     return;
36 }
37  
38 //生成订单 
39 $order_sn=build_order_no();
40 $sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price) 
41 values('$order_sn','$user_id','$goods_id','$sku_id','$price')"; 
42 $order_rs=mysql_query($sql,$conn); 
43  
44 //库存减少
45 $sql="update ih_store set number=number-{$number} where sku_id='$sku_id'";
46 $store_rs=mysql_query($sql,$conn); 
47 if(mysql_affected_rows()){ 
48     insertLog('库存减少成功');
49 }else{ 
50     insertLog('库存减少失败');
51 }
posted @ 2020-03-05 10:33  时光一寸灰  阅读(208)  评论(0编辑  收藏  举报