文件index.php
<?phprequire_once './myRedis.php';require_once './function.php';class sendAward{ 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($type==self::V1){ $this->way1(self::V1); }elseif($type==self::V2){ $this->way2(self::V2); }else{ return ''; } } //抢购商品办法一 protected function way1($v){ $redis = new myRedis($this->conf); $keyNmae = getKeyName($v); if(!$redis->exists($keyNmae)){ $redis->set($keyNmae,0); } $currAmount = $redis->get($keyNmae); if(($currAmount+self::INCRAMOUNT)>self::AMOUNTLIMIT){ writeLog(\公众没有抢到商品\"大众,$v); return; } $redis->incrby($keyNmae,self::INCRAMOUNT); writeLog(\"大众抢到商品\"大众,$v); } //抢购商品办法二 protected function way2($v){ $redis = new myRedis($this->conf); $keyNmae = getKeyName($v); if(!$redis->exists($keyNmae)){ $redis->setnx($keyNmae,0); } if($redis->incrby($keyNmae,self::INCRAMOUNT) > self::AMOUNTLIMIT){ writeLog(\"大众没有抢到商品\"大众,$v); return; } writeLog(\公众抢到商品\"大众,$v); } }//实例化调用对应实行方法$type = isset($_GET['v'])?$_GET['v']:'way1';$conf = [ 'host'=>'192.168.0.214','port'=>'6379', 'auth'=>'test','db'=>2,];new sendAward($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(isset($conf['auth'])){
$this->handler->auth($conf['auth']); //密码验证
}
//选择数据库
if(isset($conf['db'])){
$this->handler->select($conf['db']);//选择数据库2
}else{
$this->handler->select(0);//默认选择0库
}
}
//获取key的值
public function get($name){
return $this->handler->get($name);
}
//设置key的值
public function set($name,$value){
return $this->handler->set($name,$value);
}
//判断key是否存在
public function exists($key){
if($this->handler->exists($key)){
return true;
}
return false;
}
//当key不存在的设置key的值,存在则不设置
public function setnx($key,$value){
return $this->handler->setnx($key,$value);
}
//将key的数值增加指天命值
public function incrby($key,$value){
return $this->handler->incrBy($key,$value);
}
}
文件function.php
<?php//获取商品key名称function getKeyName($v){ return \"大众send_goods_\"大众.$v;}//日志写入方法function writeLog($msg,$v){ $log = $msg.PHP_EOL; file_put_contents(\公众log/$v.log\"大众,$log,FILE_APPEND);}1.ab工具并发测试way1方法
[root@localhost oversend]# 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/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.0.213 (be patient)Completed 100 requestsCompleted 200 requestsFinished 200 requestsServer Software: nginxServer Hostname: 192.168.0.213Server Port: 8083Document Path: /index.php?v=way1Document Length: 0 bytesConcurrency Level: 100Time taken for tests: 0.089 secondsComplete requests: 200Failed requests: 0Write errors: 0Total transferred: 30600 bytesHTML transferred: 0 bytesRequests per second: 2243.13 [#/sec] (mean)Time per request: 44.581 [ms] (mean)Time per request: 0.446 [ms] (mean, across all concurrent requests)Transfer rate: 335.16 [Kbytes/sec] 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 served 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 oversend]# 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/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.0.213 (be patient)Completed 100 requestsCompleted 200 requestsFinished 200 requestsServer Software: nginxServer Hostname: 192.168.0.213Server Port: 8083Document Path: /index.php?v=way2Document Length: 0 bytesConcurrency Level: 100Time taken for tests: 0.087 secondsComplete requests: 200Failed requests: 0Write errors: 0Total transferred: 31059 bytesHTML transferred: 0 bytesRequests per second: 2311.68 [#/sec] (mean)Time per request: 43.259 [ms] (mean)Time per request: 0.433 [ms] (mean, across all concurrent requests)Transfer rate: 350.58 [Kbytes/sec] 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 served 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的在这方面就表示上风了。