首页 > 作文

PHP+redis实现的限制抢购防止商品超发功能详解

更新时间:2023-04-08 07:31:52 阅读: 评论:0

本文实例讲述了php+redis实现的限制抢购防止商品超发功能。分享给大家供大家参考,具体如下:

redis不仅仅是单纯的缓存,它还有一些特殊的功能,在一些特殊场景上很好用。redis中key的原子自增incrby和判断key不存在再写入的tnx方法,可以有效的防止超发。下面使用两个不同的方式来说明利用redis做商品购买库存数量限制。业务场景很简一路阳光到草原单,就是限制抢购5个商品,模拟并发请求抢购商品,每抢购一次对应redis中的key值增加一次,通过判断限购的数量来限制抢购氧化铁和稀盐酸,抢购成功写入成功日志,失败写入失败的信息记录,通过记录的数量来判断是否超发。

文件index.php

<?phprequire_once './myredis.php';require_once './function.php';class ndaward{  public $conf = [];  const v1 = 'way1';//版本一  const v2 = 'way2';//版本二  const amountlimit = 5;//抢购数量限制  const incramount = 1;//redis递增数量值  //初始化调用对应方法执行商品发放  public function __construct($conf,$type){    $this->conf = $conf;    if(empty($type))      return '';    if($ty英语翻译专业pe==lf::v1){      $this->way1(lf::v1);    }elif($type==lf::v2){      $this->way2(lf::v2);    }el{      return '';    }  }  //抢购商品方式一  protected function way1($v){    $redis = new myredis($this->conf);       $keynmae = getkeyname($v);    if(!$redis->exists($keynmae)){      $redis->t($keynmae,0);    }    $curramount = $redis->get($keynmae);    if(($curramount+lf::incramount)>lf::amountlimit){      writelog("没有抢到商品",$v);      return;    }    $redis->incrby($keynmae,lf::incramount);    writelog("抢到商品",$v);  }  //抢购商品方式二  protected function way2($v){    $redis = new myredis($this->conf);    $keynmae = getkeyname($v);    if(!$redis->exists($keynmae)){      $redis->tnx($keynmae,0);    }    if($redis->incrby($keynmae,lf::incramount) > lf::amountlimit){      writelog("没有抢到商品",$v);      return;    }    writelog("抢到商品",$v);  }}//实例化调用对应执行方法$type = ist($_get['v'])?$_get['v']:'way1';$conf = [  'host'=>'192.168.0.214','port'=>'6379',  'auth'=>'test','db'=>2,];new ndaward($conf,$type);

文件myredis.php

<?php/** * @desc 自定义redis操作类 * **/class myredis{  public $handler = null;  public function __construct($conf){    $this->handler = new redis();    $this->handler->connect($conf['host'], $conf['port']); //连接redis    //设置密码    if(ist($conf['auth'])){      $this->handler->auth($conf['auth']); //密码验证    }    //选择数据库    if(ist($conf['db'])){      $this->handler->lect($conf['db']);//选择数据库2    }el{      $this->handler->lect(0);//默认选择0库    }  }  //获取key的值  public function get(篮球教练员培训$name){    return $this->handler->get($name);  }  //设置key的值  public function t($name,$value){    return $this->handler->t($name,$value);  }  //判断key是否存在  public function exists($key){    if($this->handler->exists($key)){      return true;    }    return fal;  }  //当key不存在的设置key的值,存在则不设置  public function tnx($key,$value){    return $this->handler->tnx($key,$value);  }  //将key的数值增加指定数值  public function incrby($key,$value){    return $this->handler->incrby($key,$value);  }}

文件function.php

<?php//获取商品key名称function getkeyname($v){  return "nd_goods_".$v;}//日志写入方法function writelog($msg,$v){  $log = $msg.php_eol;  file_put_contents("log/$v.log",$log,file_append);}

1.ab工具并发测试way1方法

[root@localhost overnd]# ab -c 100 -n 200 http://192.168.0.213:8083/index.php?v=way1this is apachebench, version 2.3 <$revision: 655654 $>copyright 1996 adam twiss, zeus technology ltd, http://www.zeustech.net/licend to the apache software foundation, http://www.apache.org/benchmarking 192.168.0.213 (be patient)completed 100 requestscompleted 200 requestsfinished 200 requestsrver software:    nginxrver hostname:    192.168.0.213rver port:      8083document path:     /index.php?v=way1document length:    0 bytesconcurrency level:   100time taken for tests:  0.089 condscomplete requests:   200failed requests:    0write errors:      0total transferred:   30600 byteshtml transferred:    0 bytesrequests per cond:  2243.13 [#/c] (mean)time per request:    44.581 [ms] (mean)time per request:    0.446 [ms] (mean, across all concurrent requests)transfer rate:     335.16 [kbytes/c] receivedconnection times (ms)       min mean[+/-sd] median  maxconnect:    0  6  2.2   5   17processing:   2  28 16.3   25   55waiting:    1  26 15.2   24   50total:     5  34 16.3   30   60percentage of the requests rved within a certain time (ms) 50%   30 66%   35 75%   54 80%   56 90%   57 95%   60 98%   60 99%   60 100%   60 (longest request)

v1方法日志分析

[root@localhost log]# less -n way1.log    1 抢到商品   2 抢到商品   3 抢到商品   4 抢到商品   5 抢到商品   6 抢到商品   7 没有抢到商品   8 没有抢到商品   9 没有抢到商品   10 没有抢到商品   11 没有抢到商品   12 没有抢到商品

观察日志发现 抢到商品的记录有6条超过正常的5条,说明超发了

2.ab工具并发测试way2方法

[root@localhost overnd]# ab -c 100 -n 200 http://192.168.0.213:8083/index.php?v=way2this is apachebench, version 2.3 <$revision: 655654 $>copyright 1996 adam twiss, zeus technology ltd, http://www.zeustech.net/licend to the apache software foundation, http://www.apache.org/benchmarking 192.168.0.213 (be patient)completed 100 requestscompleted 200 requestsfinished 200 requestsrver software:    nginxrver hostname:    192.168.0.213rver port:      8083document path:     /index.php?v=way2document length:    0 bytesconcurrency level:   100time taken for tests:  0.087 condscomplete requests:   200failed requests:    0write errors:      0total transferred:   31059 byteshtml transferred:    0 bytesrequests per cond:  2311.68 [#/c] (mean)time per req高考分数查询uest:    43.259 [ms] (mean)time per request:    0.433 [ms] (mean, across all concurrent requests)transfer rate:     350.58 [kbytes/c] receivedconnection times (ms)       min mean[+/-sd] median  maxconnect:    0  6  5.4   5   13processing:   3  31 16.6   30   70waiting:    1  30 16.6   30   70total:     5  37 18.5   32   82percentage of the requests rved within a certain time (ms) 50%   32 66%   41 75%   45 80%   50 90%   68 95%   80 98%   81 99%   82 100%   82 (longest request)

v2方法日志分析

[root@localhost log]# less -n v2.log [root@localhost log]# less -n way2.log    1 抢到商品   2 抢到商品   3 抢到商品   4 抢到商品   5 没有抢到商品   6 抢到商品   7 没有抢到商品   8 没有抢到商品   9 没有抢到商品   10 没有抢到商品

总结:观察日志可知抢到商品的日志记录是5条并没有超发,说明利用这种方式可以限制住库存的数量。之所以超发是因为方法一中通过加法来判断限制条件的同时,并发一大,就会越过这个判断条件出现会超发,redis的在这方面就体现优势了。

本文发布于:2023-04-08 07:30:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/6c5bbfdaa89b3bb0875d15dc9437358f.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP+redis实现的限制抢购防止商品超发功能详解.doc

本文 PDF 下载地址:PHP+redis实现的限制抢购防止商品超发功能详解.pdf

标签:抢到   商品   方法   日志
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图