我正在使用 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}
但显然是错误的。
最佳答案
我找到了解决方案,问题出现在我如何在 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});
关于c++ - RedisClient 通过 EVAL 命令使用 Lua 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738779/