9951 explained code solutions for 126 technologies


predisHow can I use Redis to rate limit requests in PHP?


Rate limiting is a technique used to limit the number of requests that can be made in a given period of time. Redis can be used to rate limit requests in PHP by using the Redis SETEX command. This command sets a key with an expiration time and returns a success or failure message depending on whether the key already exists.

// Set the key with the expiration time
$key = "rate_limit_".$user_id;
$expiration_time = 3600;
$success = $redis->setex($key, $expiration_time, "1");

// Check if the key is set
if($success) {
    // The key is set and the request is allowed
} else {
    // The key is not set and the request is not allowed
}

The above code sets a key with an expiration time of one hour and returns a success or failure message depending on whether the key already exists. If the key exists, the request is allowed, otherwise it is not.

Code explanation

  • $key: This is the key that will be set in Redis.
  • $expiration_time: This is the expiration time for the key in seconds.
  • $success: This is the boolean value returned from the SETEX command which indicates whether the key was set or not.
  • setex(): This is the Redis command used to set a key with an expiration time.

Helpful links

Edit this code on GitHub