照着老师的写了v1版本的,在windows上用ab测试了好多次都没有出现问题,为啥?
<?php
require './vendor/autoload.php';
use Predis\Client;
function getRedisClient(){
$server = array(
'host' => '127.0.0.1',
'port' => 6379
);
return new Client($server);
}
function getKeyName($v){
return "mycounter_".$v;
}
function writeLog($msg,$v){
$log = $msg.PHP_EOL;
file_put_contents("log/$v.log", $log,FILE_APPEND);
}
function v1(){
$amountLimit = 100;
$keyName = getKeyName('v1');
$redis = getRedisClient();
$incrAmount = 1;
if(!$redis->exists($keyName)){
$redis->set($keyName,95);
}
$currAmount = $redis->get($keyName);
if($currAmount+$incrAmount>$amountLimit){
writeLog('Bad Luck','v1');
return;
}
writeLog('Good Luck','v1');
$redis->incrby($keyName,$incrAmount);
}
v1();