SET
Introduction
In Dragonfly, as well as in Redis and Valkey, the SET command is used to set the value of a string key.
This command may also accept several options for controlling its behavior, such as setting an expiration time or only setting the value if the key doesn't already exist.
It is one of the most fundamental and frequently used commands when storing string data in key-value pairs.
Syntax
SET key value [NX | XX] [GET] [EX seconds | PX milliseconds |
EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
- Time complexity: O(1)
- ACL categories: @write, @string, @slow
Parameter Explanations
key: The key where the value will be stored.value: The value to be set for the given key.NX(optional): Only set the key if it does not already exist.XX(optional): Only set the key if it already exists.GET(optional): Return the old value stored at key before setting to the new value.EX seconds|PX milliseconds(optional): Set the expiration time for the key in seconds or milliseconds, respectively.EXAT timestamp|PXAT milliseconds-timestamp(optional): Set the expiration for the key based on absolute Unix timestamps (seconds or milliseconds).KEEPTTL(optional): Retain the existing time-to-live (TTL) of the key, resetting only its value.
Return Values
- The command returns
OKif the operation was successful. - If a conditional option was used (e.g.,
NXorXX), and the condition wasn't met, the return value will be(nil). - When the
GEToption is used, the return value will be the old string stored at the key, ornilif no previous value existed.