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
More of Predis
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How do I use yum to install php-redis?
- How can I use Predis with a cluster in PHP?
- How do I install and configure a PHP Redis DLL on a Windows machine?
- How can I install and configure PHP and Redis on a Windows system?
- How can I use PHP to increment values in Redis using ZINCRBY?
- How can I check the version of PHP and Redis I am using?
- How can I use Redis with the Yii PHP framework?
- How do I install PHP Redis on Ubuntu 20.04?
- How can I use PHP and Redis to parse JSON data?
See more codes...