As from Redis 6.2.0, we can use the COPY
command whenever we need to copy a key’s value to another key.
Example
Suppose we set the following key:
SADD names Meow Fluffy Scratch
Result:
(integer) 3
In this case, I created a set with three members.
Let’s take a look:
SMEMBERS names
Result:
1) "Meow" 2) "Scratch" 3) "Fluffy"
We can now use the COPY
command to copy this key:
COPY names logins
Result:
(integer) 1
Here, I copied the names
key to one called logins
.
Let’s check the contents of the new key:
SMEMBERS logins
Result:
1) "Meow" 2) "Scratch" 3) "Fluffy"
As expected, it contains the same members that our names
key contains.
Note that if the destination key already exists, nothing is copied. However we can overcome this by using the REPLACE
option. See Redis COPY
Command Explained for an example.