How to copy values from one list into another in Redis?

A server-side Lua script is more convenient that a WATCH/MULTI/EXEC block to implement this kind of operation.

Here is an example of a script with takes two lists (source and destination) as parameters, and two integers defining a range in the source list. It then pushes the corresponding items to the destination list.

> rpush foo 1 2 3 4
(integer) 4
> rpush bar x
(integer) 1
> eval "local res = redis.call( 'lrange', KEYS[1], ARGV[1], ARGV[2] ); return redis.call( 'rpush', KEYS[2], unpack(res) ); "  2  foo bar 0 -1
(integer) 5
> lrange bar 0 -1
1) "x"
2) "1"
3) "2"
4) "3"
5) "4"

if you want to move the key to a new key, you can use RENAME command , the only will change the key name RENAME COMMAND

Tags:

Redis