Reputation: 528
I'm looking for an INSERT
script counterpart in Redis where I want to set multiple keys at once.
SET foo bar
SET sun moon
SET fire water
...
How would a Lua script for the above look like as I couldn't find much help online.
Upvotes: 3
Views: 2019
Reputation: 3693
For a Lua script, I would do something like so:
for i=1, #KEYS do
redis.call("SET", KEYS[i], ARGV[i])
end
Which in execution, would look like this:
EVAL 'for i=1, #KEYS do redis.call("SET", KEYS[i], ARGV[i]) end' 2 key1 key2 val1 val2
Note that #KEYS
is not dynamically calculated, but rather the explicitly passed numkeys
argument.
Additional validation could be added as necessary—asserting equal numbers of keys and args, for example—but I would strongly encourage doing most of that sanity checking client-side for performance.
If not using Lua, Redis has the command MSET
to set multiple keys at once natively.
https://redis.io/commands/mset
Upvotes: 3