直接上代码,限制单位时间内只能访问多少次
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$key = "download";
$check = $redis->setnx($key,1);
if(!$check)
{
$redis->incr($key);
$count = $redis->get($key);
if($count > self::REQUEST_LIMIT)
{
$num = self::REQUEST_LIMIT;
$this->formatReturn(new BasicReturn(false, 40010, "请求超过限制,60s只能访问{$num}次"));
}
}
else
{
$redis->expire($key,60);
}