Skip to content

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.

Terminal window
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

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.

Terminal window
SET mykey "Hello"
GET mykey

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
Terminal window
SET anotherkey "will expire in a minute" EX 60
SET yetanotherkey "will expire at a specific time" PXAT 1689876543000

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
Terminal window
SET key1 "value1" NX // Set only if key1 does not exist
SET key2 "value2" XX // Set only if key2 exists

The GET option allows returning the old value while setting a new value:

Terminal window
SET key1 "old_value"
SET key1 "new_value" GET // Returns "old_value"

In redisun, the SET command is implemented through the SetCommand class and the set method in the Redisun class.

Redisun redisun = Redisun.create(options -> {
options.setHost("localhost");
options.setPort(6379);
});
// Basic setting
boolean result = redisun.set("mykey", "myvalue");
// Setting with options
boolean result = redisun.set("mykey", "myvalue", command -> {
command.expire(60); // Expires in 60 seconds
});
// Asynchronous setting
CompletableFuture<Boolean> future = redisun.asyncSet("mykey", "myvalue");
// Asynchronous setting with options
CompletableFuture<Boolean> future = redisun.asyncSet("mykey", "myvalue", command -> {
command.expire(60); // Expires in 60 seconds
});
// Using various SetCommand options
redisun.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
});
  1. The options supported by the SET command can replace commands such as SETNX, SETEX, PSETEX, GETSET, which may be deprecated in future versions.
  2. 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.
  3. When using the GET option, if the key exists but is not of string type, an error will be returned.