EXPIRE
Set a timeout on key in seconds.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”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
Detailed Explanation
Section titled “Detailed Explanation”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.
Return Value
Section titled “Return Value”- Returns 1 if set successfully
- Returns 0 if the key does not exist or the condition is not met
Time Complexity
Section titled “Time Complexity”O(1)
Redisun Usage
Section titled “Redisun Usage”// Synchronous modeint result = redisun.expire("mykey", 60);
// Synchronous mode with optionsint result = redisun.expire("mykey", 60, cmd -> cmd.setIfNotExists());
// Asynchronous modeCompletableFuture<Integer> future = redisun.asyncExpire("mykey", 60);
// Asynchronous mode with optionsCompletableFuture<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