TTL command in redis

TTL key

The TTL command returns the remaining time to live in seconds of a key that has an EXPIRE set.

This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.

Note

1. If the key does not have an associated expiration, -1 is returned.

2. If the key does not exist, -2 is returned.

RETURN VALUE: Integer reply 

Syntax:

TTL key

Example 1: When the key is present and set with the expiry time.

redis:6379> SET redisKey 1000
"OK"
redis:6379> EXPIRE redisKey 100
(integer) 1
redis:6379> TTL redisKey
(integer) 89
redis:6379> TTL redisKey
(integer) 80


Example 2: When the key is present and expiry time is not set.

redis:6379> SET redisKey2 1000
"OK"
redis:6379> TTL redisKey2
(integer) -1

Output

-1


Example 3: When the key is not present.

redis:6379> TTL myredis
(integer) -2

Output

-2


No comments:

Post a Comment