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
$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 = $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
More of Predis
- How can I use the zscan command in PHP with Redis?
- How do I use the PHP Redis zrevrange command?
- How do I install PHP, Redis, and XAMPP?
- How can I troubleshoot a "PHP Redis went away" error?
- How can I set a timeout for a Redis connection using PHP?
- How do I install and configure a PHP Redis DLL on a Windows machine?
- How do I install PHP Redis on Ubuntu 20.04?
- How can I use PHP and Redis to get keys based on a pattern?
- How can I use PHP and Redis to parse JSON data?
See more codes...