SET
The SET
command in DiceDB is used to set the value of a key. If the key already holds a value, it is overwritten, regardless of its type. This is one of the most fundamental operations in DiceDB as it allows for both creating and updating key-value pairs.
Syntax
Parameters
-
key
: The name of the key to be set.- Type: String
- Required: Yes
-
value
: The value to be set for the key.- Type: String
- Required: Yes
-
EX seconds
: Set the specified expire time, in seconds.- Type: Integer
- Required: No
-
PX milliseconds
: Set the specified expire time, in milliseconds.- Type: Integer
- Required: No
-
NX
: Only set the key if it does not already exist.- Type: None
- Required: No
-
XX
: Only set the key if it already exists.- Type: None
- Required: No
-
KEEPTTL
: Retain the time-to-live associated with the key.- Type: None
- Required: No
Return Value
- Returns
OK
if the command is successful. - Returns
nil
if theNX
orXX
conditions are not met. - Returns an error in cases where the syntax or specified constraints are invalid.
Behaviour
- If the specified key already exists, the
SET
command will overwrite the existing key-value pair with the new value unless theNX
option is provided. - If the
NX
option is present, the command will set the key only if it does not already exist. If the key exists, no operation is performed andnil
is returned. - If the
XX
option is present, the command will set the key only if it already exists. If the key does not exist, no operation is performed andnil
is returned. - Using the
EX
orPX
options together withKEEPTTL
is not allowed and will result in an error. - When provided,
EX
sets the expiry time in seconds andPX
sets the expiry time in milliseconds. - The
KEEPTTL
option ensures that the key’s existing TTL is retained.
Error Handling
The command may raise errors in the following scenarios:
-
Wrong type of value or key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs when attempting to use the command on a key that contains a non-string value.
- Error Message:
-
Invalid syntax or conflicting options
:- Error Message:
(error) ERR syntax error
- Occurs if the command’s syntax is incorrect, such as incompatible options like
EX
andKEEPTTL
used together, or a missing required parameter.
- Error Message:
-
Non-integer value for
EXor
PX“:- Error Message:
(error) ERR value is not an integer or out of range
- Occurs when the expiration time provided is not a valid integer.
- Error Message:
Example Usage
Basic Usage
Setting a key foo
with the value bar
:
Using Expiration Time (in seconds)
Setting a key foo
with the value bar
to expire in 10 seconds:
Using Expiration Time (in milliseconds)
Setting a key foo
with the value bar
to expire in 10000 milliseconds (10 seconds):
Setting Only if Key Does Not Exist
Setting a key foo
only if it does not already exist:
Response
:
- If the key does not exist:
OK
- If the key exists:
nil
Setting Only if Key Exists
Setting a key foo
only if it exists:
Response
:
- If the key exists:
OK
- If the key does not exist:
nil
Retaining Existing TTL
Setting a key foo
with a value bar
and retaining existing TTL:
Invalid Usage
Trying to set key foo
with both EX
and KEEPTTL
will result in an error: