Computer >> Computer tutorials >  >> Programming >> Redis

How to get expiration time of key in redis – Redis TTL | PTTL

In this tutorial, we will learn about how to get an expiration time ( timeout ) of key by using redis TTL and PTTL command..

Expiration Time in Seconds :-

To get an expiration time of key in seconds, we will use a COMMAND – TTL in redis-cli. This number of seconds represents the remaining time to live of the key, after this time key will get deleted from datastore. The syntax of command TTL is as follows :-

Syntax :-

redis host:post> TTL <key name>

Output :-

 2 if the key does not exist
 1 if the key exists but has no expiration time
 (integer) <time in seconds>

Example :-

How to get expiration time of key in redis – Redis TTL | PTTL

Expiration Time in Milliseconds :-

To get an expiration time of key in milliseconds, we will use a COMMAND – PTTL in redis-cli. PTTL command is very similar to TTL command with difference is that, in PTTL timestamp is in milliseconds while in TTL timestamp is in seconds. The syntax of command is as follows :-

Syntax :-

redis host:post> PTTL <key name>

Output :-

 2 if the key does not exist 
 1 if the key exists but has no expiration time
 (integer) <time in milliseconds>

Example :-

How to get expiration time of key in redis – Redis TTL | PTTL

References :-

  1. TTL Command Docs
  2. PTTL Command Docs

That’s all for how to get expiration time of a key in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.