Redis TTL Command
The Redis TTL command returns the remaining time to live of a key in seconds.
Syntax
The basic syntax of the redis TTL command is as follows:
redis 127.0.0.1:6379> TTL KEY_NAME
Available Versions
= 1.0.0
Return Value
- Returns -2 if the key does not exist.
- Returns -1 if the key exists but has no associated expire time.
- Otherwise, returns the remaining time to live of the key in seconds.
Note: Prior to Redis 2.8, the command returned -1 both when the key did not exist and when the key existed but had no associated expire time.
Examples
# Non-existent key
redis> FLUSHDB
OK
redis> TTL key
(integer) -2
# Key exists but has no associated expire time
redis> SET key value
OK
redis> TTL key
(integer) -1
# Key with an associated expire time
redis> EXPIRE key 10086
(integer) 1
redis> TTL key
(integer) 10084