简体   繁体   English

StackExchange.Redis - 如何克隆哈希映射

[英]StackExchange.Redis - how to clone hash map

是否有任何 StackExchange.Redis api 来克隆哈希映射或更好地与 redis lua 脚本一起使用?

The easiest way to clone/copy any Redis data type is with the DUMP and RESTORE commands combo.克隆/复制任何 Redis 数据类型的最简单方法是使用DUMPRESTORE命令组合。 It is also the fastest in most cases.在大多数情况下,它也是最快的。

To avoid sending the payload back and forth, a Lua script definitely the best way for that ( https://gist.github.com/itamarhaber/d30b3c40a72a07f23c70 ):为了避免来回发送有效负载,Lua 脚本绝对是最好的方法( https://gist.github.com/itamarhaber/d30b3c40a72a07f23c70 ):

-- @desc:   The fastest, type-agnostic way to copy a Redis key
-- @usage:  redis-cli --eval copy_key.lua <source> <dest> , [NX]

local s = KEYS[1]
local d = KEYS[2]

if redis.call("EXISTS", d) == 1 then
  if type(ARGV[1]) == "string" and ARGV[1]:upper() == "NX" then
    return nil
  else
    redis.call("DEL", d)
  end
end

redis.call("RESTORE", d, 0, redis.call("DUMP", s))
return "OK"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM