Created
August 15, 2023 05:01
-
-
Save esemeniuc/dbf01b396f6e7afcf609d8dc5e715351 to your computer and use it in GitHub Desktop.
Redis Chunking (avoid unpack() stack issues)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local keys = redis.call('KEYS', ARGV[1]); | |
if next(keys) == nil then return keys end; | |
local chunk_size = 4096; -- must be under 8k, see https://github.com/nhibernate/NHibernate-Caches/issues/62 | |
local n = #keys; | |
local out = {}; | |
for i=1,n,chunk_size do | |
local unpack_start = i; | |
local unpack_end = math.min(n, i + chunk_size - 1); | |
local vals = redis.call('MGET', unpack(keys, unpack_start, unpack_end)); | |
for j=unpack_start,unpack_end do | |
local key_idx = j -- always sequentially increasing, 1..n | |
local val_idx = j-unpack_start+1 -- needs to index into vals array, 1..chunk_size | |
out[key_idx] = {keys[key_idx], vals[val_idx]}; | |
end | |
end | |
return out; | |
local keys = redis.call('KEYS', ARGV[1]); | |
if next(keys) == nil then return keys end; | |
local out = {}; | |
for i=1,#keys do out[i] = {keys[i], redis.call('GET', keys[i])} end | |
return out; | |
local chunk_size = 5 | |
local n = 2 | |
for i=1,n,chunk_size do | |
local unpack_start = i; | |
local unpack_end = math.min(n, i + chunk_size - 1); | |
for j=unpack_start,unpack_end do | |
local key_idx = j -- always sequentially increasing, 1..n | |
local val_idx = j-unpack_start+1 -- needs to index into vals array, 1..chunk_size | |
print(key_idx, val_idx); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment