Skip to content

Instantly share code, notes, and snippets.

@esemeniuc
Created August 15, 2023 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esemeniuc/dbf01b396f6e7afcf609d8dc5e715351 to your computer and use it in GitHub Desktop.
Save esemeniuc/dbf01b396f6e7afcf609d8dc5e715351 to your computer and use it in GitHub Desktop.
Redis Chunking (avoid unpack() stack issues)
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