Laravel 5.5 ThrottleRequest 中间件

Laravel 5.5 ThrottleRequest middleware

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

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

我知道如何将它应用到路线,我只是不确定什么是合理的参数。

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

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

如果你想限制一些 activity 每 5 分钟 10 次点击,那么 decayMinutes 必须是 5。