使用 Lua 脚本和 EVAL 命令的 RedisClient

我正在使用 nekipelov/redisclient 访问 Redis 并且我需要一次性地检索多个哈希数据,以提高性能。

更具体地说,我正在尝试检索以下多个哈希:

redis-cli --ldb --eval /tmp/script.lua hash_key1 hash_key2

其中,script.lua 如下所示:

local r = {}
for _, v in pairs(KEYS) do
r[#r+1] = redis.call('HGETALL', v)
end
return r

但我在通过 nekipelov/redisclient 使用 EVAL 命令来表达上述内容时遇到了困难。

我尝试了以下内容:

redisclient.command("EVAL", {"/tmp/script.lua", hash_key1, hash_key2}

但显然是错误的。

点赞
用户789646
用户789646

我找到了解决方案,问题出在我在 redisclient 中如何构建 EVAL 命令上——我以文件形式传递了 Lua 脚本:

const std::string script =
            "local r = {} "
            "for _, v in pairs(KEYS) do "
            "r[#r+1] = redis.call('HGETALL', v) "
            "end "
            "return r ";

const unsigned int numKeys = 2;
const std::string key1 = "hash_key1";
const std::string key2 = "hash_key2";

result = redisclient.command("EVAL", {script, std::to_string(numKeys), key1, key2});
2017-01-19 10:35:34