Skip to content

Instantly share code, notes, and snippets.

@Andrey2G
Last active February 20, 2018 00:35
Show Gist options
  • Save Andrey2G/2497e4057da3774d3b8de2e1991502ab to your computer and use it in GitHub Desktop.
Save Andrey2G/2497e4057da3774d3b8de2e1991502ab to your computer and use it in GitHub Desktop.
Redis, LUA, execute Redis command in chunks
-- to avoid the problem with unpack ("too many results to unpack")
-- we can increase LUAI_MAXCSTACK in luaconf.h https://www.lua.org/source/5.1/luaconf.h.html
-- or execute the command with using table.unpack (https://www.lua.org/manual/5.1/manual.html#pdf-table.unpack) in chunks by specific length
-- table.merge - separated function to merge two tables
-- size of args should be >4000 (chunk length)
local executeRedisCommandInChunks = function (command, key, args)
local results = {}
local chunkResult = {}
local args_len = #args
-- chunk length (any positive value less then 8000)
local chunk_len = 4000
if (string.len(key)>0) then
-- for commands with one key like HMGET/HMSET
for i = 1, args_len, chunk_len do
chunkResult = redis.call(command, key, unpack(args, i, math.min(i + chunk_len - 1, args_len)))
results = table.merge(results,chunkResult)
end
else
-- for commands like DEL/MGET/MSET/etc
for i = 1, args_len, chunk_len do
chunkResult = redis.call(command, unpack(args, i, math.min(i + chunk_len - 1, args_len)))
results = table.merge(results,chunkResult)
end
end
return results
end