When working with Redis, we have a number of options for setting keys that store string values.
Here are six ways to set a string value in Redis.
The SET
Command
If we only need to set one key, the SET
command is a good candidate:
SET user "Chump"
Result:
OK
If the key is set successfully, we get an OK
.
We can now use commands like GET
to check the value of the key:
GET user
Result:
"Chump"
The SET
command allows us to include options that specify things like timeouts for the key, whether to set it if it already exists, or doesn’t exist, and there’s even an option that returns the old value (if the key already existed).
See Redis SET
Command Explained for more information and examples.
The MSET
Command
If we have multiple keys to set, the MSET
command can help us set them all in one go:
MSET user "Ali" membership "Gold" logins 12
Result:
OK
We get an OK
when the keys are set.
We can use the MGET
command to check the values of all keys at the same time:
MGET user membership logins
Result:
1) "Ali" 2) "Gold" 3) "12"
See Redis MSET
Command Explained for more information and examples.
There’s also an MSETNX
command that sets the key only if it doesn’t already exist.
The COPY
Command
Redis has a COPY
command that enables us to copy a key to another key. Therefore, it technically allows us to “set” a key by copying its value from another key:
COPY user username
Result:
(integer) 1
Here, we copied the user
from the previous example, to a new key called username
.
Let’s check the contents of the new key:
GET username
Result:
"Ali"
By default, if the destination key already exists, the value is not copied. However, this can be changed by using the REPLACE
option.
See Redis COPY
Command Explained for more info and examples.
The RESTORE
Command
Redis has a RESTORE
command that creates a key associated with a value that is obtained by deserialising the provided serialised value (obtained via the DUMP
command). We can therefore use this to set a new key:
RESTORE topuser 0 "\x00\x03Ali\n\x00\xa0\x11Pf\xc2)K\x97"
Result:
OK
In this case, I created a key called topuser
and restored the serialised value to it. I also set a ttl
of 0
, which means that it has no expiry.
Let’s check the value of our new topuser
key:
GET topuser
Result:
"Ali"
By default, if the key already exists, an error is returned. But we can use the REPLACE
argument to change this, so that the existing key is replaced with the new value.
See Redis RESTORE
Command Explained for more info and examples.
The INCR
Command
In Redis, the INCR
command increments the value of a key by 1
:
INCR logins
Result:
(integer) 13
Here, we incremented the logins
key that we set in an earlier example. The key already had a value of 12, and so our INCR
operation incremented that to 13
, then returned that number as an integer reply.
If we run it again, it increments again:
INCR logins
Result:
(integer) 14
If the key doesn’t exist, it’s created with a value of 0
, then incremented by 1
:
INCR newkey
Result:
(integer) 1
Redis has a bunch of other related commands that we can use for incrementing/decrementing values. For example, there’s the following:
INCRBY
– Increment the key by a specified amountINCRBYFLOAT
– Increment a floating point number by a specified amountDECR
– Decrement the keyDECRBY
– Decrement the key by a specified amount
The SETRANGE
Command
In Redis, the SETRANGE
command allows us to overwrite part of the string stored at a given key. If the key doesn’t exist, it’s created with the specified value.
SETRANGE country 0 "South Africa"
Result:
(integer) 12
When we use SETRANGE
, we specify the key name, the offset, then the new value that will start from that offset. In this case, the key didn’t already exist and I specified an offset of 0
. This basically has the effect of setting the new key/value pair.
But the main purpose of SETRANGE
is to update a specific part of the current value. For example, we can update the above value as follows:
SETRANGE country 6 "Korea"
Result:
(integer) 12
Let’s check the current value:
GET country
Result:
"South Koreaa"
We can see that its value has been updated as expected.
If we look closely, we can see that there’s an extra a
at the end of the string. That’s because the substring we set is only 5 characters, when there were 6 characters until the end of the string.