Several times in one move - Redis

How can we use lua scripts to implement multi-get.

Say if I installed name_lastBeckham and name_firstDavid. What should be a lua script in order to get both name_last, and name_firstat a time?

I can implement something like:

eval "return redis.call('get',KEYS[1])" 1 foo

to get the value of a single key. Just think about how to increase this part of the scripts to get values ​​that apply to all keys (or several keys) by simply making one call to redis server .

+4
source share
2 answers

, , EVAL (0 , KEYS ARGV):

eval "..." 0 name_last name_first

-, , MGET:

local values = redis.call('MGET', unpack(ARGV))

-, ( ):

local results = {}
for i, key in ipairs(ARGV) do
  results[key] = values[i]
end
return results

, , :

eval "local values = redis.call('MGET', unpack(ARGV)); local results = {}; for i, key in ipairs(ARGV) do results[key] = values[i] end; return results" 0 name_last name_first
+4

KEYS GET , . :

local t = []
for _, k in pairs(KEYS) do
  t[#t+1] = redis.call('GET', k)
end
return t

P.S. MGET btw:)

+2

All Articles