9951 explained code solutions for 126 technologies


predisHow can I use PHP and Redis to set a time-to-live (TTL) value?


To use PHP and Redis to set a time-to-live (TTL) value, first you need to connect to the Redis server using the phpredis extension.

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

Then, you can set a time-to-live (TTL) value for a key by using the SETEX command. The SETEX command takes three parameters: the key name, the TTL value in seconds, and the value to set.

$key = 'mykey';
$ttl = 60; // expire in 60 seconds
$value = 'myvalue';

$redis->setex($key, $ttl, $value);

You can then check the TTL value of the key by using the TTL command.

$ttl = $redis->ttl($key);

echo "TTL of $key is $ttl seconds\n";
// Output: TTL of mykey is 59 seconds

You can also delete a key by using the DEL command.

$redis->del($key);

Helpful links

Edit this code on GitHub