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
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the key to be set. | String | Yes |
value | The value to be set for the key. | String | Yes |
EX | Set the specified expire time, in seconds. | Integer | No |
EXAT | Set the specified Unix time at which the key will expire, in seconds | Integer | No |
PX | Set the specified expire time, in milliseconds. | Integer | No |
PXAT | Set the specified Unix time at which the key will expire, in milliseconds | Integer | No |
NX | Only set the key if it does not already exist. | None | No |
XX | Only set the key if it already exists. | None | No |
KEEPTTL | Retain the time-to-live associated with the key. | None | No |
GET | Return the value of the key before setting it. | None | No |
Return values
Condition | Return Value |
---|---|
Command is successful | OK |
NX or XX conditions are not met | nil |
Syntax or specified constraints are invalid | error |
If the GET option is provided | The value of the key before setting it or error if value cannot be returned as a string |
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
,EXAT
,PX
orPXAT
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. - The
GET
option can be used to return the value of the key before setting it. If the key does not exist,nil
is returned. If the key exists but does not contain a value which can be returned as a string, an error is returned. The set operation is not performed in this case.
Errors
-
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