RedisClient to use Lua script with EVAL command

I use nekipelov / redisclient to access Redis, and I need to get multiple hash data with a single Redis call to improve performance.

More specifically, I am trying to extract multiple hashes as shown below:

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

where script.lua:

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

But it's hard for me to express this using the EVAL command via nekipelov / redisclient.

I tried something below:

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

but obviously wrong.

+4
source share
1 answer

I found a solution and there was a problem with the way I built the EVAL command in redisclient - I passed the Lua script as a file:

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});
+1
source

Source: https://habr.com/ru/post/1667249/


All Articles