I decided to compile a list of commands that can be used to get the string value from a key in Redis. These are all string commands – commands that can be used when working with strings. In other words, the assumption is that the key holds a string value.
The GET
Command
The most obvious command is the GET
command. This command simply gets the value of the given key.
Example:
GET color
Result:
"Blue"
Here, the key called color
holds a string value of Blue
.
The MGET
Command
The MGET
command is similar to GET
, except that it allows us to get the value from more than one key at a time. All we do is pass each key name, separated by a space:
MGET color plant url
Result:
1) "Blue" 2) "Tree" 3) (nil)
This command returns its result in an array reply.
In my case, the first two keys hold a string value, but the third key doesn’t exist. When a key doesn’t exist, nil
is returned (the same applies to the GET
command).
The GETDEL
Command
The GETDEL
command gets the value of the key and deletes the key:
GETDEL plant
Result:
"Tree"
The key’s value is returned as expected.
Now let’s try running the command again:
GETDEL plant
Result:
(nil)
We get nil
as expected because the key no longer exists.
The GETEX
Command
The GETEX
command is similar to GET
, except that it allows us to set various options for the key. In particular, it provides options for setting/changing the expiry associated with the key.
Example:
GETEX color EX 86400
Result:
"Blue"
Here, I returned the value of the color
key, and set its expiry to 86400 seconds (the equivalent of a day). We can alternatively specify the expiry in milliseconds, or as a Unix timestamp (either in seconds or milliseconds), and we can also remove any expiry associated with the key (by using the PERSIST
option).
The GETRANGE
Command
The GETRANGE
command returns a substring from the given string. So, we can use this command to return a substring or the whole string, depending on the value of the arguments we provide.
Example:
GETRANGE color 0 3
Result:
"Blue"
In this case, I returned the whole string. However, I can return a substring if I pass different arguments:
GETRANGE color 1 2
Result:
"lu"
The SUBSTR
Command
The SUBSTR
command is deprecated. It does the same thing that GETRANGE
does, so use the GETRANGE
command instead.
Even though this command is deprecated, here’s an example anyway:
SUBSTR color 1 2
Result:
"lu"
As we can see by the example, we get the same result as GETRANGE
when we provide the same arguments.
The GETSET
Command
The GETSET
command is deprecated. It does the same thing that SET
does with the GET
argument, so use the SET
command instead.
Even though this command is deprecated, here’s an example anyway:
GETSET color "Yellow"
Result:
"Blue"
This command sets the key to the specified value and returns the old value. So in this example we can see that the old value is Blue
and we set its new value to Yellow
.
Let’s use GET
to check the current value:
GET color
Result:
"Yellow"
As expected, it’s the value that we specified.
As mentioned, it’s best to avoid this command because it is now deprecated. Use SET
with the GET
option instead.