使用RedisClient和EVAL命令执行Lua脚本

4

我正在使用nekipelov/redisclient访问Redis,我需要一次调用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}

但显然是错误的。

1个回答

1
我找到了解决方案,问题出现在我如何构造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});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接