Laravel 5.2 过期时间的缓存增量问题

Laravel 5.2 Cache increment issue with expiration time

我在第一个 http 请求时将密钥添加到缓存 x 分钟,然后在下一个请求中继续递增。

我面临的问题是在递增时,缓存过期时间也被重置,我需要保留过期时间。我还以秒为单位显示到期时间。

下面是我的代码:

$key = $input['device_id'];
if(! $attempts = Cache::get($key))
{
   Cache::put($key, 1, Carbon::now()->addMinutes(5));
}
else
{
  Cache::increment($key);
}

您可以扩展内置中间件 ThrottlesRequests 并将其附加到您的登录路由:

 class MyThrottleRequests extends \Illuminate\Routing\Middleware\ThrottlesRequests {

      protected function resolveRequestSignature($input) {
              return $input->device_id; //I think this is what you mean to use right?
      }

 }

然后您可以在 Kernel.php

中定义的中间件中指定它
  protected $routeMiddleware = [
       // Existing ones
       'throttleDeviceId' => MyThrottleRequests::class
  ]; 

然后您可以在您需要的路线上使用它:

  \Route::any("/route/to/throttle",/* Route definition */)->middleware("throttle:<max requests>,<within time>");