Skip to content

SETEX

The SETEX command in DiceDB is used to set the value of a key and its expiration time in seconds. This command is atomic and is commonly used to create time-sensitive key-value pairs.

Syntax

Terminal window
SETEX key seconds value

Parameters

ParameterDescriptionTypeRequired
keyThe name of the key to be set.StringYes
secondsExpiration time for the key in seconds.IntegerYes
valueThe value to be set for the key.IntegerNo

Return values

ConditionReturn Value
Command is successfulOK
Syntax or specified constraints are invaliderror

Behaviour

  • The SETEX command sets the value of a key and specifies its expiration time in seconds.
  • If the specified key already exists, the value is overwritten, and the new expiration time is set.
  • If the key does not exist, it is created with the specified expiration time.
  • If the provided expiration time is invalid or not an integer, the command will return an error.
  • This command is equivalent to using SET key value EX seconds but provides a more concise and dedicated syntax.

Errors

  1. Missing or invalid expiration time:

    • Error Message: (error) ERR value is not an integer or out of range
    • Occurs if the expiration time is not a valid positive integer.
  2. Missing required arguments:

    • Error Message: (error) ERR wrong number of arguments for 'SETEX' command
    • Occurs if any of the required arguments (key, seconds, or value) are not provided.

Example Usage

Basic Usage

Set a key foo with the value bar to expire in 10 seconds:

Terminal window
127.0.0.1:7379> SETEX foo 10 bar
OK

Set a key foo with the value new_value, overwriting the existing value and resetting the expiration time:

Terminal window
127.0.0.1:7379> SETEX foo 20 new_value
OK

Invalid usage

Setting a key with an invalid expiration time will result in an error:

Terminal window
127.0.0.1:7379> SETEX foo -10 bar
(error) ERROR invalid expire time in 'setex' command

Attempting to use the command with missing arguments will result in an error:

Terminal window
127.0.0.1:7379> SETEX foo 10
(error) ERROR wrong number of arguments for 'setex' command

Notes:

SETEX can be replaced via SET with EX option.