PushedCrayon
PushedCrayon

Reputation: 123

How to write a lua script to get multiple keys at once from Redis?

I am trying to write a Lua script that will return multiple keys back from a Redis DB. The script I am trying to run is:

script load "local values = redis.call('MGET', unpack(ARGV)); local results = {}; for i, key in ipairs(ARGV) do results[key] = values[i] end; return results"

I would then try and run it using evalsha (whatever the sha number is that it returns) 0 dog cat (where dog and cat are two keys I have saved in my DB).

Ideally, it would return "woof" "meow" as those are the values stored in the keys dog and cat. For some reason, this script will always return an empty list or set, and I do not understand why. Any help figuring it out would be appreciated!

Upvotes: 1

Views: 2804

Answers (1)

for_stack
for_stack

Reputation: 22936

results should be an array, i.e. indexed with number, NOT string. See conversion between Lua and Redis data types for details.

In order to make it work, change results[key] = values[i] into results[i] = values[i].

Also, in fact, you can just return values, there's no need to convert values to results.

Upvotes: 2

Related Questions