9951 explained code solutions for 126 technologies


predisHow do I set an expiration time for a Redis key using PHP?


Using PHP, you can set an expiration time for a Redis key by calling the setex() command. This command takes three parameters: the key, the expiration time in seconds, and the value.

For example, the following code sets the key mykey to expire in 10 seconds:

$redis->setex('mykey', 10, 'myvalue');

The output of this command is true if the key was set successfully.

Code explanation

  1. $redis: the Redis instance
  2. setex(): the Redis command for setting an expiration time
  3. mykey: the key to set
  4. 10: the expiration time in seconds
  5. myvalue: the value to set

Helpful links

Edit this code on GitHub