Skip to content

EXPIRE

Set a timeout on key in seconds.

Terminal window
EXPIRE key seconds [NX | XX | GT | LT]

Parameter Description

  • key: The key to set expiration time
  • seconds: Expiration time (seconds)
  • NX: Set only if the key has no expiration time
  • XX: Set only if the key already has an expiration time
  • GT: Set only if the new expiration time is greater than the current expiration time
  • LT: Set only if the new expiration time is less than the current expiration time

The EXPIRE command sets an expiration time for the given key. After the expiration time is reached, the key will be automatically deleted.

The GT, LT, and NX options are mutually exclusive and cannot be used together.

Keys without an expiration time are considered to have infinite TTL in GT and LT comparisons.

  • Returns 1 if set successfully
  • Returns 0 if the key does not exist or the condition is not met

O(1)

// Synchronous mode
int result = redisun.expire("mykey", 60);
// Synchronous mode with options
int result = redisun.expire("mykey", 60, cmd -> cmd.setIfNotExists());
// Asynchronous mode
CompletableFuture<Integer> future = redisun.asyncExpire("mykey", 60);
// Asynchronous mode with options
CompletableFuture<Integer> future = redisun.asyncExpire("mykey", 60, cmd -> cmd.setIfExists());

Notes

  • Expiration time is in seconds
  • If the key does not exist, the command will return 0
  • You can view the remaining TTL of a key through the TTL command
  • GT, LT, and NX options are mutually exclusive

References