I thought I’d compile a list of Redis commands that can be used to set strings. Each command has its own specific use, and so the command used will depend on the situation.
In any case, here are 14 commands that set strings in Redis.
The SET
Command
The SET
command sets a given key to a specific string value:
SET animal "Cat"
Result:
OK
This command returns OK
when the value is successfully set.
The SET
command also accepts a bunch of optional arguments that allow us to do more than just set the string. For example, there are arguments that allow us to set the expiry of the key. And there’s also a GET
option that enables us to return the old string that was stored at the key before we set it to the new value.
Here’s an example of using the GET
option when setting the key to a different value:
SET animal "Dog" GET
Result:
"Cat"
In this case, I set the key to Dog
and returned the old value (which is Cat
).
We can check the new value with the GET
command:
GET animal
Result:
"Dog"
The key now contains Dog
as expected.
The SETNX
Command
The SETNX
command is similar to SET
, except that it only sets the key if it doesn’t already exist.
Here’s an example of setting a key that doesn’t already exist:
SETNX country "Mexico"
Result:
(integer) 1
The integer reply of 1
indicates that the key was successfully set (it didn’t previously exist).
Here’s what happens if we try to set the key again:
SETNX country "Sweden"
Result:
(integer) 0
This time we get an integer reply of 0
, which means that the key wasn’t set. That’s because it already exists.
The MSET
Command
The MSET
command allows us to set multiple keys:
MSET animal "Bird" plant "Tree"
Result:
OK
In this case I set the animal
key and the plant
key.
There’s also an MGET
command that allows us to return the value of multiple keys. Let’s use it now to check our keys:
MGET animal plant
Result:
1) "Bird" 2) "Tree"
Exactly as expected.
The MSETNX
Command
Redis also has an MSETNX
command, which sets the keys only if none of them already exist:
MSETNX animal "Zebra" color "Blue"
Result:
(integer) 0
In the above example, the color
key doesn’t exist but the animal
key does, therefore nothing was set.
Let’s change the first key to one that doesn’t exist:
MSETNX pet "Zebra" color "Blue"
Result:
(integer) 1
This time all keys were set. We know this because we got an integer reply of 1
.
The SETEX
Command
The SETEX
command sets a key and its expiry in seconds:
SETEX animal 60 "Buffalo"
Result:
OK
Here, I set the key to Buffalo
, with an expiry of 60 seconds. This means that the key will no longer exist after 60 seconds (unless it’s reset or its expiry is changed).
The PSETEX
Command
The PSETEX
command is similar to SETEX
, except that the expiry is in milliseconds:
SETEX animal 60000 "Antelope"
Result:
OK
Here, I set the key to Antelope
, with an expiry of 60000 milliseconds. The key will no longer exist after 60000 seconds (unless it’s reset or its expiry is changed).
The INCR
Command
The INCR
command increments a value stored at a given key by one:
INCR counter
Result:
(integer) 1
If the key doesn’t exist, it’s created with a value of 0
, then incremented by 1
. This is what happened in my example above, and we can see this with the integer reply of 1
.
Let’s run the code again:
INCR counter
Result:
(integer) 2
Now we get an integer reply of 2
, which is the key’s current value.
The INCRBY
Command
The INCRBY
command increments a value stored at a given key by the specified amount:
INCRBY counter 10
Result:
(integer) 12
Here, I incremented the existing value by 10
, so the key now holds a value of 12
.
The INCRBYFLOAT
Command
The INCRBYFLOAT
command increments a value stored at a given key by the specified floating point number:
INCRBYFLOAT counter 2.57
Result:
14.57
Here, I incremented the existing value by 2.57
, so the key now holds a value of 14.57
.
The DECR
Command
The DECR
command decrements a value stored at a given key by one:
DECR points
Result:
(integer) -1
Here, I decremented a key that didn’t exist. Therefore, it was created, set to zero, and decremented by one.
Let’s decrement it again:
DECR points
Result:
(integer) -2
The DECRBY
Command
The DECRBY
command decrements a value stored at a given key by the specified amount:
DECRBY points 10
Result:
(integer) -12
The APPEND
Command
The APPEND
command appends the specified value to the end of a string. If the key doesn’t exist, it is created with the value:
APPEND colors "Blue"
Result:
(integer) 4
This command returns the length of the string after the append operation. In my case, the key didn’t exist and so it was created with the specified value. The value is four characters long, and so I get an integer reply of 4
.
The SETRANGE
Command
The SETRANGE
command overwrites part of the string stored at the specified key. However, if the key doesn’t exist, it’s created with the specified value:
SETRANGE countries 0 "Belize"
Result:
(integer) 6
This command returns an integer reply of the length of the string after the operation has been applied. In my case, the key didn’t exist and so it was created with the specified value. Therefore, the string’s length matches that of the value that I set.
The GETSET
Command
I was hesitant to include this command, because it is now deprecated (due to the SET
command being able to provide the same functionality). But here it is anyway.
The GETSET
command does the same thing that we did previously with the GET
option of the SET
command – it sets a given key and returns its old value:
GETSET animal "Bird"
Result:
"Dog"
So this time I set the key to Bird
and it returned its old value of Dog
.
Let’s check the key’s current value:
GET animal
Result:
"Bird"
Exactly as expected.
As alluded to, it’s best to avoid this command due to it being deprecated. Better to use the SET
command with the GET
option.