log/v1.log 没有信息
<?php
require_once('vendor/autoload.php');
use Predis\Client;
//$redis = new \Redis();
//$redis->connect('127.0.0.1', 7200);
function getKeyName($v)
{
return 'mycounter_'.$v;
}
function getRedisClient()
{
return new Client([
'host' => '127.0.0.1',
'port'=> '7200',
]);
}
function writeLog($msg, $v)
{
$log = $msg . PHP_EOL;
file_put_contents("Log/$v.log", $log, LOCK_EX|FILE_APPEND);
}
function v1()
{
$amountLimit = 100;
$keyName = getKeyName('v1');
$redis =getRedisClient();
$incrAmount = 1;
if(1$redis->exists($keyName)){
$redis->set($keyName,95);
}
$currAmount = $redis->get($keyName);
if(($currAmount + $incrAmount)> $amountLimit){
writeLog('bad luck','v1');
return;
}
$redis->incrby($keyName, $incrAmount);
writeLog('Good Luck','v1');
function v2()
{
$amountLimit = 100;
$keyName = getKeyName('v2');
$redis =getRedisClient();
$incrAmount = 1;
if(1$redis->exists($keyName)){
$redis->setnx($keyName,95);
}
$currAmount = $redis->get($keyName);
if($redis->incrby($keyName, $incrAmount) > $amountLimit){
writeLog('bad luck','v2');
return;
}
writeLog('Good Luck','v2');
}
if($_GET['v'] == 2 ){
v2();
}else{
v1();
}