skip to Main Content

So I want to give a limit to requests that require a key, and a limit of 100x requests in 60 minutes (for a trial of 10x requests in 5 seconds) and will return (reset) to 0.
but i tried this code, and after 5 seconds the limit is not reset and requests are still limited.

I try this code :

        $key = $request->apikey;
        $apiKey = ApiKey::where('key', $key)->first();
        if (!$apiKey) {
            return response()->json(['message' => 'Invalid API key'], 401);
        }

        $rateLimit = 10;
        $timer = 5;

        $requests = $apiKey->ignore_limits ?? 0;
        $lastRequestTime = Cache::get($key . ':timer');
        dd(Cache::has($key));
        if ($lastRequestTime && (time() - $lastRequestTime) > ($timer)) {
            $requests = 0;
        } else {
            if ($requests >= $rateLimit) {
                return response()->json(['message' => 'Rate limit exceeded'], 429);
            }
            $requests = $apiKey->ignore_limits ?? $requests;
        }

        $apiKey->ignore_limits = $requests + 1;
        $apiKey->save();
        Cache::put($key, $requests, now()->addSeconds($timer));
        Cache::put($key . ':timer', time(), now()->addSeconds($timer));

        return $next($request);

2

Answers


  1. Chosen as BEST ANSWER

    this my final code works and the descriptions,

       $rateLimit = 10; // Limit Request / timer
            $timer = 5; // Timer (Second)
    
            $requests = Cache::get($key, 0); // Count a cache
            $lastRequestTime = Cache::get($key . ':timer'); // Cache time (not null)
            if ($lastRequestTime && (time() - $lastRequestTime) > $timer) { // If (time() now - Cache $lastRequestTime) > $timer
                Cache::put($key, 0, now()->addSeconds($timer)); // Reset the request count accordance to $timer
                $requests = 0; // Reset the $requests
            } else {
                if ($requests >= $rateLimit) { // if count a cache ($requests) >= $rateLimit(10) return
                    return response()->json(['message' => 'Rate limit exceeded'], 429);
                }
                Cache::increment($key); // Increase the request count 1
            }
    
            Cache::put($key . ':timer', time(), now()->addSeconds($timer)); // Update the request time
    
            return $next($request);
    

  2. I’ve made some adjustments to your code to make it correctly implement rate limiting:

      $key = $request->apikey;
    $apiKey = ApiKey::where('key', $key)->first();
    
    if (!$apiKey) {
        return response()->json(['message' => 'Invalid API key'], 401);
    }
    
    $rateLimit = 10;
    $timer = 5;
    
    $requests = Cache::get($key, 0);
    $lastRequestTime = Cache::get($key . ':timer');
    
    if ($lastRequestTime && (time() - $lastRequestTime) > $timer) {
        Cache::put($key, 0, now()->addMinutes(1)); // Reset the request count
    } else {
        if ($requests >= $rateLimit) {
            return response()->json(['message' => 'Rate limit exceeded'], 429);
        }
        Cache::increment($key); // Increase the request count
    }
    
    Cache::put($key . ':timer', time(), now()->addMinutes(1)); // Update the request time
    
    return $next($request);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search