Latest web development tutorials

Redis TTL command

Redis key (key)

Redis TTL command returns the key in seconds remaining expiration time.

grammar

redis TTL basic command syntax is as follows:

redis 127.0.0.1:6379> TTL KEY_NAME

Available versions

> = 1.0.0

return value

When the key does not exist, it returns -2. When the key is present but not set the remaining lifetime, it returns -1. Otherwise, the remaining survival time, in milliseconds, the return key.

Note: Redis 2.8 ago, when the key does not exist or is not set key remaining lifetime, the command returns -1.

Examples

# 不存在的 key

redis> FLUSHDB
OK

redis> TTL key
(integer) -2


# key 存在,但没有设置剩余生存时间

redis> SET key value
OK

redis> TTL key
(integer) -1


# 有剩余生存时间的 key

redis> EXPIRE key 10086
(integer) 1

redis> TTL key
(integer) 10084

Redis key (key)