利用redis限制访问频率

直接上代码,限制单位时间内只能访问多少次

        $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);
        }

猜你喜欢

转载自blog.csdn.net/zjr11092/article/details/82117646