首页 文章

Laravel 5.5 ThrottleRequest中间件

提问于
浏览
5

有没有人知道如何在Laravel 5.5中实现 ThrottleRequest 中间件?

我不清楚 decayMinutes 参数的含义:https://laravel.com/api/5.5/Illuminate/Routing/Middleware/ThrottleRequests.html

我理解如何将它应用到路线,我只是不确定什么是可重复的参数 .

2 回答

  • 2

    decayMinutes - 这是你的限制内的时间将被计算在内 . 技术上限制是缓存中TTL(生存时间) $decayMinutes * 60 秒的值,每次命中都会递增 . 当TTL超过值时,将自动在缓存中销毁,并且将开始新的命中计数 .

    看看RateLimit::hit()代码 . 很清楚:

    /**
     * Increment the counter for a given key for a given decay time.
     *
     * @param  string  $key
     * @param  float|int  $decayMinutes
     * @return int
     */
    public function hit($key, $decayMinutes = 1)
    {
        $this->cache->add(
            $key.':timer', $this->availableAt($decayMinutes * 60), $decayMinutes
        );
        $added = $this->cache->add($key, 0, $decayMinutes);
        $hits = (int) $this->cache->increment($key);
        if (! $added && $hits == 1) {
            $this->cache->put($key, 1, $decayMinutes);
        }
        return $hits;
    }
    

    如果要将某些活动限制为每5分钟10次点击,则 decayMinutes 必须为5 .

  • 6

    我理解 decayMinutes 为保留时间 . 对于intance,如果您想尝试使用错误的密码登录,但如果他尝试11次,则用户将被阻止 decayMinutes 中指定的分钟数 . 如果您指定10分钟作为 decayMinutes ,则用户将被阻止10分钟

相关问题