0
点赞
收藏
分享

微信扫一扫

PHP-限流-TokenBucket

沪钢木子 2022-02-08 阅读 63
public function checkLimit($cacheKey = '', $initNum = '', $expire = '', $retry = true)
{
    $nowTime = time();
    Redis::watch($cacheKey);
    $redisData = Redis::get($cacheKey);
    $limitData = $redisData ? json_decode($redisData, true) : ['num' => $initNum, 'time' => $nowTime];

    // (单位时间访问频率 / 单位时间)*(当前时间 - 上次访问时间) = 上次请求至今可增加的访问次数
    $addNum = intval(($initNum / $expire) * ($nowTime - $limitData['time']));
    $newNum = min($initNum, (($limitData['num'] - 1) + $addNum));
    Log::debug($cacheKey . '-剩余领牌次数:' . $newNum);
    if ($newNum <= 0) {
        return ['status' => false, 'msg' => '当前时刻令牌用完啦!'];
    }
    $limitData = json_encode(['num' => $newNum, 'time' => $nowTime]);
    Redis::multi();
    Redis::set($cacheKey, $limitData);
    if (!Redis::exec()) {
        if (!$retry) {
            return ['status' => false, 'msg' => '访问频次过多!'];
        } else {
            //执行3次丢弃
            for ($i = 0; $i < 3; $i++) {
                $aRet = self::_limit($cacheKey, $initNum, $expire, false);
                if (Arr::get($aRet, 'status', false)) {
                    break;
                }
                if (Arr::get($aRet, 'msg') == '当前时刻令牌用完啦') {
                    return $aRet;
                }
            }
        }
    }
    return ['status' => true, 'msg' => 'ok'];
}
举报

相关推荐

PHP-循环

PHP-遍历对象

PHP-$$变量覆盖

PHP-包含文件

PHP-命名空间

PHP-文件编程

PHP-判断语句

PHP-学习知识整理

0 条评论