Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis TTL Command

Redis TTL Command

Redis Key

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 Version

= 1.0.0

Return Value

Returns -2 if key does not exist. Returns -1 if key exists but has no remaining time to live. Otherwise, returns the remaining time to live of the key in seconds.

**Note:** Before Redis 2.8, the command returned -1 when key does not exist or when key has no remaining time to live set.

Example



# Non-existent key



redis> FLUSHDB

OK



redis> TTL key

(integer) -2





# key exists, but no remaining time set



redis> SET key value

OK



redis> TTL key

(integer) -1





# key with remaining survival time



redis> EXPIRE key 10086

(integer) 1



redis> TTL key

(integer) 10084

Redis Key