SET
The SET command is used to set the value of a specified key. If the key already exists, regardless of its type, it will be overwritten. After successfully executing a SET operation, any associated time-to-live (TTL) with the key will be discarded.
The time complexity of the SET command is O(1), making it a very fast operation.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”SET key value [NX | XX] [GET] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]Parameter Description
- key: The key to set
- value: The value to set
- NX: Set the key only if it does not exist
- XX: Set the key only if it already exists
- GET: Return the old value stored at the key, or nil if the key does not exist
- EX seconds: Set the key’s expiration time (in seconds)
- PX milliseconds: Set the key’s expiration time (in milliseconds)
- EXAT timestamp-seconds: Set the key to expire at a specific UNIX timestamp (in seconds)
- PXAT timestamp-milliseconds: Set the key to expire at a specific UNIX timestamp (in milliseconds)
- KEEPTTL: Retain the time-to-live associated with the key
Detailed Explanation
Section titled “Detailed Explanation”Basic Usage
Section titled “Basic Usage”The SET command is one of the most fundamental Redis string operation commands. It sets the key to the specified string value. If the key already exists, the SET command will overwrite the old value, regardless of the old value’s type.
SET mykey "Hello"GET mykeyExpiration Time Options
Section titled “Expiration Time Options”The SET command supports multiple options for setting expiration time:
- EX seconds: Set the key to expire after the specified number of seconds
- PX milliseconds: Set the key to expire after the specified number of milliseconds
- EXAT timestamp-seconds: Set the key to expire at a specific UNIX timestamp (in seconds)
- PXAT timestamp-milliseconds: Set the key to expire at a specific UNIX timestamp (in milliseconds)
- KEEPTTL: Retain the key’s existing expiration time
SET anotherkey "will expire in a minute" EX 60SET yetanotherkey "will expire at a specific time" PXAT 1689876543000Conditional Setting Options
Section titled “Conditional Setting Options”The SET command supports conditional setting options:
- NX: Set only if the key does not exist (equivalent to the SETNX command)
- XX: Set only if the key already exists
SET key1 "value1" NX // Set only if key1 does not existSET key2 "value2" XX // Set only if key2 existsGet Old Value Option
Section titled “Get Old Value Option”The GET option allows returning the old value while setting a new value:
SET key1 "old_value"SET key1 "new_value" GET // Returns "old_value"Redisun Usage
Section titled “Redisun Usage”In redisun, the SET command is implemented through the SetCommand class and the set method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setHost("localhost"); options.setPort(6379);});
// Basic settingboolean result = redisun.set("mykey", "myvalue");
// Setting with optionsboolean result = redisun.set("mykey", "myvalue", command -> { command.expire(60); // Expires in 60 seconds});Asynchronous Usage
Section titled “Asynchronous Usage”// Asynchronous settingCompletableFuture<Boolean> future = redisun.asyncSet("mykey", "myvalue");
// Asynchronous setting with optionsCompletableFuture<Boolean> future = redisun.asyncSet("mykey", "myvalue", command -> { command.expire(60); // Expires in 60 seconds});Using SetCommand Options
Section titled “Using SetCommand Options”// Using various SetCommand optionsredisun.set("mykey", "myvalue", command -> { command.setIfNotExists(); // NX option command.expire(60); // EX option});
redisun.set("mykey", "myvalue", command -> { command.setIfExists(); // XX option command.expireMs(60000); // PX option});
redisun.set("mykey", "myvalue", command -> { command.keepTTL(); // KEEPTTL option});
redisun.set("mykey", "myvalue", command -> { command.expireAt(new Date(System.currentTimeMillis() + 60000)); // PXAT option});- The options supported by the SET command can replace commands such as SETNX, SETEX, PSETEX, GETSET, which may be deprecated in future versions.
- Redis uses double-precision floating-point numbers to represent scores, and integers within the range of -(2^53) to +(2^53) can be precisely represented.
- When using the GET option, if the key exists but is not of string type, an error will be returned.