Skip to content

TTL

Get the remaining time to live of a key in seconds.

Terminal window
TTL key

Parameter Description

  • key: The key to query expiration time

The TTL command returns the remaining time to live of the given key in seconds.

  • Returns -2 when the key does not exist
  • Returns -1 when the key exists but has no expiration time set
  • Otherwise returns the remaining expiration time of the key in seconds

O(1)

// Synchronous mode
long ttl = redisun.ttl("mykey");
// Asynchronous mode
CompletableFuture<Long> future = redisun.asyncTtl("mykey");

Notes

  • The TTL command can only be used to query the expiration time of a key, not modify it
  • For keys without expiration time set, the TTL command returns -1
  • For non-existent keys, the TTL command returns -2

References