EXPIREAT
The EXPIREAT
command is used to set the expiration time of a key in DiceDB. Unlike the EXPIRE
command, which sets the expiration time in seconds from the current time, EXPIREAT
sets the expiration time as an absolute Unix timestamp (in seconds). This allows for more precise control over when a key should expire.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the key to be set. | String | Yes |
timestamp | The unix-time-seconds timestamp at which the key should expire. This is an integer. | Integer | Yes |
NX | Set the expiration only if the key does not already have an expiration time. | None | No |
XX | Set the expiration only if the key already has an expiration time. | None | No |
GT | Set the expiration only if the new expiration time is greater than or equal to the current one. | None | No |
LT | Set the expiration only if the new expiration time is less than the current one. | None | No |
Return Values
Condition | Return Value |
---|---|
Timeout was successfully set. | 1 |
Timeout was not set (e.g., key does not exist, or conditions not met). | 0 |
Behaviour
- When the
EXPIREAT
command is executed, DiceDB will set the expiration time of the specified key to the given Unix timestamp. - If the key already has an expiration time, it will be overwritten with the new timestamp.
- If the key does not exist, no timeout is set, and the command returns
0
. - Conditional flags (NX, XX, GT, LT) control when the expiry can be set based on existing timeouts.
Errors
-
Syntax Error
:- Error Message:
(error) ERROR wrong number of arguments for 'expireat' command
- Returned if the command is issued with an incorrect number of arguments.
- Error Message:
-
Invalid Timestamp
:- Error Message:
(error) ERROR value is not an integer or out of range
- Returned if the timestamp is not a valid integer.
- Error Message:
-
Invalid Unix Timestamp Format
:- Error Message:
(error) ERROR invalid expire time in 'EXPIREAT' command
- Returned if the timestamp is outside the supported range.
- Error Message:
Example Usage
Basic Usage
This example demonstrates setting a key to expire at a specific Unix timestamp.
Key does not exist
Trying to set an expiration time for a non-existing key.
Setting an EXPIRYTIME with NX option
Here, the NX
option is used to set the expiration time only if the key does not already have an expiration time.
Setting an EXPIRYTIME with XX option
Here, the XX
option is used to set the expiration time only if the key already has an expiration time.
Setting an EXPIRYTIME with GT option
The GT
option is used to set the expiration time only if the new expiration time is greater than or equal to the current one.
Setting an EXPIRYTIME with LT option
Similar to the GT
option, the LT
option is used to set the expiration time only if the new expiration time is less than (or equal to) the current one.
Best Practices
- Use
TTL
command to check remaining time before expiration - Consider using
PERSIST
command to remove expiration if needed - Choose appropriate conditional flags (NX, XX, GT, LT) based on your use case
- Ensure Unix timestamps are in seconds, not milliseconds
- Be aware of the timestamp limit of [9223372036854775]
Alternatives
- Use
EXPIRE
command for simpler expiration control based on relative time