PHP接口访问频率限制

发布时间:2021/03/03 作者:天马行空 阅读(1781)

如果要限制某个接口一分钟之内只能访问10次。有很多人都会想当然的通过redis设置一个过期时间为1分钟的key,那么这样会存在一个问题。在59秒的时候接口访问了10次,然后key马上就过期了,下一秒访问的时候又可以访问10次,这样根本就没有达到1分钟之内访问10次的需求。


直接上代码

function apiVisitLimit($userId) {
    $key = "api:limit:userId:{$userId}";
    $redis = new Redis();
    $redis->connect('127.0.0.1');
    $data = $redis->hGetAll($key);
    echo '<pre>';print_r($data);
    //需要删除的key
    $del_key = [];
    //时间内访问的总次数
    $total = 0;
    //时间内最大访问次数
    $max_frequency = 10;
    //当前时间
    $now_time = time();
    //限制时间
    $limit_time = 60;
    foreach ($data as $time=>$count) {
        if ($time < $now_time - $limit_time) {
            $del_key[] = $time;
        } else {
            $total += $count;
        }
    }
    //存在需要删除的key
    if ($del_key) {
        $redis->hDel($key, ...$del_key);
    }
    if ($total >= $max_frequency) {
        return false;
    }
    //不能用hSet、hSetNx,因为如果存在会返回false,同一秒钟访问多次会出问题
    return $redis->hIncrBy($key, $now_time, 1);
}
$userId = 1;
$result = apiVisitLimit($userId);
if (!$result) {
    die('操作过于频繁');
}


关键字php redis