Redis Command Basics - Part 2
In this second installment of this multi-part article we’ll show you more Redis command basics so you continue to feel more and more comfortable working with Redis.
More Essential Redis Commands for Key-Value Pairs
In the first part of this tutorial we learned the basic SET, GET, DEL, EXISTS, INCR, and DECR commands. We’ll learn a couple more to our repertoire.
Similar to the INCR command which increases a value by one, we can also increase the value by a certain amount with the INCRBY command. Let’ see it in action. We’ll also show the SET and GET here as a refresher of what we covered in Part 1:
Now we haven’t told you the syntax of the command but notice that after you type the command INCRBY, the command line will let you know in grey what it’s expecting. In this case it’s expecting a key followed by an increment:
So let’s follow it’s lead and increase our
var by 100 like so:
DECRBY works in the same fashion as INCRBY and would look like this:
If you’d like to set multiple key value pairs at the same time you’ll use the MSET command. Let’s see how we’d set two key-value pairs:
Let’s verify that it worked by GETting the values:
127.0.0.1:6379> GET var2
Note: If you run this command on existing keys it will replace the values. If you don’t want to overwrite the value check out the next command MSETNX.
If you want to set multiple key values but want to make sure you don’t replace any existing keys, then MSETNX is the command to use. If any of the keys you provide already exists it will abort the command entirely.
We’ll FLUSHALL our data so we have a fresh start then look at an example.
Now let’s set a variable:
Now let’s use MSETNX and try to overwrite var1 as well as set some other key values:
127.0.0.1:6379> GET var1
127.0.0.1:6379> GET var2
As you can see MSETNX returned a 0 meaning it failed. We then check var1 to see that it was not changed and var2 was not set. This is because since var1 already existed the entire operation was aborted and no values were set.
If you want to get multiple key values at the same time you’ll use the command MGET. For this example we’ll use MSET to set multiple key values then use MGET to read them back:
127.0.0.1:6379> MGET var1 var2
If you have a string value and want to append to a string onto it you can use the APPEND command. Let’s see an example:
127.0.0.1:6379> APPEND str1 lltop
127.0.0.1:6379> GET str1
Note: If the key you provide does not exist then it will just use that string as the value ( so it works exactly like a SET in that case).
If you want a substring of a value you’ll use GETRANGE and provide it with a key and a range.
127.0.0.1:6379> GETRANGE str 0 5
Note It does also accept negative offsets if you want to start from the end of the string.
If you want to rename a key use the RENAME command. The value will of course stay unchanged.
127.0.0.1:6379> RENAME cost price
127.0.0.1:6379> GET price
Like the other NX commands, this one performs a RENAME but only if the new key name doesn’t already exist. If the new key already exists it performs no action.
127.0.0.1:6379> SET var2 100
127.0.0.1:6379> RENAMENX var1 var2
It returns a 0 so we know the action failed.
We’ve covered a lot more commands and demos to show you exactly how they work but there is still more ground to cover so we hope you’ll join us in Part 3 of this tutorial on some of the essential Redis commands.
Pilot the ObjectRocket platform free for 30 Days
It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!PILOT FREE FOR 30 DAYS