双十一期间要做活动,设置商品请求限流,护航秒杀活动正常进行!
#设置1秒内最多同时1000请求
$maxNum = 1000;
$redisKey = 'GoldMall:Huodong:gid'.$g_id.'_'.date('s');
$onlineNum = (int)$this->redis->get($redisKey);
if($onlineNum){
$onlineNum++;
$this->redis->set($redisKey,$onlineNum,1);
}else{
$onlineNum = 1;
$this->redis->set($redisKey,$onlineNum,1);
}
if($onlineNum>$maxNum){
$this->jsonResponse(401,"服务器繁忙请稍后再试~{$onlineNum}");
}
以上代码经过实践测试,OK的!