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

StackExchange.Redis - how to clone hash map

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

clone/copy 任何 Redis 数据类型的最简单方法是使用 DUMP and RESTORE 命令组合。在大多数情况下也是最快的。

为了避免来回发送负载,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"