Last active
March 22, 2019 21:28
-
-
Save alexanderscott/3724d7ecab84f1b49ab0 to your computer and use it in GitHub Desktop.
Redis Lua script to copy a key without knowing its type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- @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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
--eval
switch.