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.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”GET keyParameter Description
- key: The key to get the value
Detailed Explanation
Section titled “Detailed Explanation”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.
Basic Usage Example
Section titled “Basic Usage Example”GET nonexistingSET mykey "Hello"GET mykeyRedisun Usage
Section titled “Redisun Usage”In redisun, the GET command is implemented through the GetCommand class and the get method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setHost("localhost"); options.setPort(6379);});
// Get the key valueString value = redisun.get("mykey");
// Handle potentially null casesif (value != null) { System.out.println("Value: " + value);} else { System.out.println("Key does not exist");}- The GET command can only be used to get string-type values
- An error will be returned for non-string-type keys
- nil is returned when the key does not exist (null in redisun)