Redis get expire time – How we find it easily?
Are you trying to get the expire time in Redis? We’ll help you with it.
Usually, the timeout on Redis keys plays an important role in website navigation sessions. And, website owners often need tweaks in timeout values too.
That’s why at Bobcares, we often receive requests regarding key expiration as part of our Server Management Services.
Today, let’s discuss this topic in detail and see how our Support Engineers set Redis timeouts easily.
More about Redis key expiration
As we all know, Redis is a server that stores data. Also, it works as a data storage space that can store, retrieve and manage data.
But, it functions primarily as a key-value store. And, it is possible to set timeout values on these keys.
In Redis terminology, the key with an associated timeout is called volatile. But, a timeout expiration automatically deletes the key. Additionally, when we use commands that delete or overwrite the contents of the key, it will clear the timeout.
Need to know the expire time in Redis?
Usually, this key expiration is useful in the case of the Navigation session on a website.
Suppose, we want to view the latest N pages visited by our users. And, these set of page views is known as Navigation session of the user.
We can do this easily in Redis by using the EXPIRE command.
Suppose, we set the timeout of the key as 60. So, if the user becomes idle for more than 60 seconds, the key will get deleted. And, it will record the subsequent page views that have less than 60 seconds of difference.
Now, let’s discuss the ways to get the expire time of the key.
How can we get the expire time in Redis?
Recently, one of our customers approached us with a requirement. He had already set a timeout of 60 on his key.
And, he wanted to know the remaining time to live of that key. This helps to identify the time to fetch the content again. Our Support Engineers used the TTL command to know the remaining time in seconds.
So, Redis TTL helps the Redis client to check how many seconds are left for the key to expire.
For instance, if we set the timeout on the key “key1” as 60, we can get the remaining time to live as follows.
Similarly, the PTTL command returns the remaining time in milliseconds. Also, both TTL and PTTL shows a value of -1, if the key does not have an expiry timeout.
And, if the key does not exist, it will show -2.
Also, we can use the expireat command to expire the key at a specific point in time.
expireat key_name time
As we have already said, using commands that overwrite the contents of the key will clear out the timeout on that key.
So, we use the persist command to manually clear a key’s timeout.
And, after its successful completion, this command will return (integer)1.
[Need more assistance with Redis timeout configuration? – We’re available 24/7 to help you.]
In short, we can get the expire time in Redis using the TTL command. In today’s article, we have discussed how our Support Engineers do it easily.