c++luaredisredisclient

RedisClient to use Lua script with EVAL command


I am using nekipelov/redisclient to access Redis and I need to retrieve multiple hash data with one call to Redis to increase performance.

More specific, I am trying to retrieve multiple hashes like 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 I have difficulty to express the above by using EVAL command through nekipelov/redisclient.

I tried something below:

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

but obviously is wrong.


Solution

  • I found the solution and the problem appeared on how I constructed the EVAL command in redisclient - I was passing 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});