PHP 高并发情况的简单模拟,thinkphp

看下面代码,一个简单的减库存模拟.

一,并发扣库存

1.原始数据里面有100个库存

 
image.png

2.下面进行减库存操作,代码如下所示

        $model = new Test();
        $data = $model->get(1);
        $count = $data['count'];
        $model->update(['id' => 1, 'count' => $count - 1]);

3.并发10,模拟100个请求

ab.exe -c 10 -n 100 http://www.tp5.net/
哈哈, 理论上count应该是0对吧,但是我们看看实际的结果

 
image.png

库存出错了吧,因为同时读这个数据的时候,会出现两个请求读到的都是90,这样后面就错了

 

4.我们利用mysql的自减函数,换一个代码试试

        $model = new Test();
$model->where('id', 1)->setDec('count');

正常了,变成0了


 
image.png
        $model = new Test();
$model->where('id', 1)->setDec('count');

二,并发新增数据

1.100个并发,这时候不在同一条数据,需要插入

        $model = new Test();
        $data = $model->order('id desc')->find();
        $count = $data['count'];
        $new_count = $count -1;
        $model->insert(['count'=>$new_count]);

ab.exe -c 100 -n 1000 http://www.tp5.net/

 
image.png

看到没有,出现了两条一模一样的数据,出bug了吧.下面用两种方法来解决

 

不好意思犯了一个错误,不要用mysql保留函数做字段名,下面改为count改为stock_num

3.文件锁

        $fp = fopen(ROOT_PATH . "./lock.txt", "w+");
        if (flock($fp, LOCK_EX)) {
            $model = new Test();
            $data = $model->order('id desc')->find();
            $stock_num = $data['stock_num'];
            $new_stock_num = $stock_num - 1;
            $model->insert(['stock_num' => $new_stock_num]);
            flock($fp, LOCK_UN);
        } else {
            return data('0', 286, "系统繁忙稍后再试");
        }
        fclose($fp);

ab.exe -c 100 -n 1000 http://www.tp5.net/

 
image.png

没有重复的,竟然解决了,实际项目中不推荐这种方法.

 

4.redis锁,其实原理和上面文件锁的原理类似

5.mysql悲观锁+事务

        $model = new Test();
        $model->startTrans();
        try {
            $data = $model->lock(true)->order('id desc')->find();
            $stock_num = $data['stock_num'];
            $new_stock_num = $stock_num - 1;
            $model->insert(['stock_num' => $new_stock_num]);
            $model->commit();
        } catch (\Exception $exception) {
            $model->rollback();
            throw $exception;
        }

6.mysql乐观锁+事务

        $model = new Test();
        $TestLogModel = new TestLog();
        $model->startTrans();
        try {
            $data = $model->order('id desc')->find();
            $stock_num = $data['stock_num'];
            $version = $data['version'];
            $new_stock_num =$stock_num-1;
            $stock_data = ['stock_num' => $new_stock_num,'version' => $version+1];
            $rows = $model->where(['id' => 1,'version' =>$version])->update($stock_data);
            if($rows == 1){
                $res = $TestLogModel->insert(['number' => $new_stock_num]);
                if($res){
                    $model->commit();
                }else{
                    throw new \Exception('插入失败');
                }
            }else{
                throw new \Exception('更新失败');
            }
        } catch (\Exception $exception) {
            $model->rollback();
            throw $exception;
        }

没有出现重复数据,但是有数据丢失了




posted @ 2022-04-20 15:32  你背后的那个人  阅读(665)  评论(0编辑  收藏  举报