Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Last active March 22, 2019 21:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderscott/3724d7ecab84f1b49ab0 to your computer and use it in GitHub Desktop.
Save alexanderscott/3724d7ecab84f1b49ab0 to your computer and use it in GitHub Desktop.
Redis Lua script to copy a key without knowing its type
-- @desc: Copy a key without knowing its type
-- @usage: redis-cli EVAL "$(cat COPY.lua)" 2 <key> <destKey>
local function COPY(key, dest)
local keyType = redis.call("TYPE", key)
if keyType == "set" then redis.call("SUNIONSTORE", dest, key)
elseif keyType == "zset" then redis.call("ZUNIONSTORE", dest, 1, key)
elseif keyType == "list" then redis.call("SORT", key, "BY", "NOSORT", "STORE", dest)
elseif keyType == "string" then redis.call("SET", dest, redis.call("GET", key))
elseif keyType == "hash" then
local hash = redis.call('HGETALL', key);
redis.call('HMSET', dest, unpack(hash));
--else return { err = "Key "..key.." does not exist or has unknown type"}
end
return
end
--[[ @TEST
redis.call("ZADD", "test_zset1", 5, "five", 6, "six")
COPY("test_zset1", "test_zset2")
assert(redis.call("ZCARD", "test_zset2") == 2)
assert(redis.call("ZSCORE", "test_zset2", "five") == 5)
redis.call("DEL", "test_zset1", "test_zset2")
redis.call("SADD", "test_set1", "five", "six")
COPY("test_set1", "test_set2")
assert(redis.call("SCARD", "test_set2") == 2)
redis.call("DEL", "test_set1", "test_set2")
redis.call("SET", "test_str1", "five")
COPY("test_str1", "test_str2")
assert(redis.call("GET", "test_str2") == "five")
redis.call("DEL", "test_str1", "test_str2")
--]]
return COPY(KEYS[1], KEYS[2])
@itamarhaber
Copy link

Cool script, but here's something that's a little nastier and much faster (stay tuned for the blog post tomorrow 😉): https://gist.github.com/itamarhaber/d30b3c40a72a07f23c70

Also:

  1. What are you using to run the tests with? I like having unit tests bundled like that.
  2. You may want to note that you can run scripts with the more convenient (IMO) --eval switch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment