复制Redis中的密钥 [英] Duplicate a key in redis

查看:87
本文介绍了复制Redis中的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用连接的redis-cli复制密钥吗,redis中是否预定义了任何命令?

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

将FSS_SYSAGENT复制到FSS_SYSAGENTDuplicate.

Duplicate FSS_SYSAGENT to FSS_SYSAGENTDuplicate.

10.44.112.213:6403> hgetall FSS_SYSAGENT

10.44.112.213:6403> hgetall FSS_SYSAGENT

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

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}"

推荐答案

您可以使用 DUMP RESTORE 命令来复制密钥:

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

  1. 使用DUMP命令序列化密钥的值.
  2. 使用RESTORE命令将序列化的值还原到另一个密钥.
  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.

您可以将以下两个步骤包装到Lua脚本中:

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

使用redis-cli运行Lua脚本:./redis-cli --eval duplicate.lua FSS_SYSAGENT FSS_SYSAGENTDuplicate ,

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

这篇关于复制Redis中的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆