-- 优化 redis 的管线操作,防止出现请求过多引发的操作失败 local function pipeline(page_size) local table_insert = table.insert page_size = page_size or 1024 assert(page_size > 0) local cache = {} local page = {} local function add(...) table_insert(page, { ... }) if #page == page_size then table_insert(cache, page) page = {} end end local function execute(redis, resp) if #page > 0 then table_insert(cache, page) page = {} end if #cache > 0 then local old = cache cache = {} for _, v in ipairs(old) do redis:pipeline(v, resp) end end return resp end return { add=add, execute=execute } end return pipeline