5

Can I duplicate a key using the redis-cli connected, is there any command predefined in redis or not?

Duplicate FSS_SYSAGENT to FSS_SYSAGENTDuplicate.

10.44.112.213:6403> hgetall FSS_SYSAGENT

1) "SYSTEM_01" 2) "{\"port\":\"4407\",\"ipAddress\":\"10.44.112.213\",\"symbolicName\":\"SYSTEM_01\",\"eventLogEnabled\":\"1110\",\"status\":1,\"wcPort\":\"6029\",\"activeSystem\":\"N\",\"createdBy\":\"\",\"createdDate\":\"2018-11-20 13:11:16\",\"modifiedBy\":\"\",\"modifiedDate\":\"\",\"institution\":\"FSS\",\"delFlag\":0,\"accessID\":0,\"rowCount\":0,\"endCount\":0}"

Manu C Rajan
  • 153
  • 1
  • 2
  • 13
  • Possible duplicate of [Redis : How to set one key equal to the value of another key?](https://stackoverflow.com/questions/10891213/redis-how-to-set-one-key-equal-to-the-value-of-another-key) – Urosh T. Nov 26 '18 at 11:32
  • @UroshT. not a duplicate. – Manu C Rajan Nov 27 '18 at 04:26

1 Answers1

7

You can use the DUMP and RESTORE commands to duplicate the key:

  1. use the DUMP command to serialize the value of a key.
  2. use the RESTORE command to restore the serialized value to another key.

You can wrap these two steps into a Lua script:

-- duplicate.lua
local src = KEYS[1]
local dest = KEYS[2]

local val = redis.call('DUMP', src)
if val == false then
    return 0
else
    -- with RESTORE command, you can also set TTL for the new key, and use the [REPLACE] option to set the new key forcefully. 
    redis.call('RESTORE', dest, 0, val)
    return 1
end

Run the Lua script with redis-cli: ./redis-cli --eval duplicate.lua FSS_SYSAGENT FSS_SYSAGENTDuplicate ,

UPDATE

Since Redis 6.2.0, you can use the COPY command to do the job.

for_stack
  • 21,012
  • 4
  • 35
  • 48