In Redis, the GET
command returns the value of a given key. If the key doesn’t exist, it returns nil
.
An error is returned if the value stored at key
is not a string.
Syntax
GET key
Where key
is the key for whose value we want returned.
Example
Suppose we set a key/value pair:
SET type "dog"
In this case, type
the key and "dog"
is its value.
Now that we’ve set the type
key, we can use the GET
command to get its value:
GET type
Result:
"dog"
As expected, the value is returned.
When the Key Doesn’t Exist
If the key doesn’t exist, we get nil
. Here’s an example to demonstrate:
GET oops
Result:
(nil)
There’s no oops
key, and so nil
was returned.
Key/value pairs can expire (if they’ve been assigned an expire time), so if you get a null result when trying to get the value of a key that you are almost certain exists, it could be that it has expired. Or it could be that it was deleted.
Since Redis 6.2.0, there’s also a GETDEL
command that gets the key and then immediately deletes it.