Skip to content

GET

The GET command is used to get the value of a specified key. If the key does not exist, nil is returned. The GET command can only handle string values. If the value stored in the key is not a string, an error will be returned.

The time complexity of the GET command is O(1), making it a very fast operation.

Terminal window
GET key

Parameter Description

  • key: The key to get the value

The GET command is one of the most basic commands in Redis, used to retrieve the string value stored in a specified key. It is the corresponding operation to the SET command.

When the key does not exist, the GET command returns the special value nil. If the key exists but the stored value is not a string (e.g., list, set, or other data types), an error will be returned.

Terminal window
GET nonexisting
SET mykey "Hello"
GET mykey

In redisun, the GET command is implemented through the GetCommand class and the get method in the Redisun class.

Redisun redisun = Redisun.create(options -> {
options.setHost("localhost");
options.setPort(6379);
});
// Get the key value
String value = redisun.get("mykey");
// Handle potentially null cases
if (value != null) {
System.out.println("Value: " + value);
} else {
System.out.println("Key does not exist");
}
  1. The GET command can only be used to get string-type values
  2. An error will be returned for non-string-type keys
  3. nil is returned when the key does not exist (null in redisun)