What's the recommended way to repeat SCAN in Lua for Redis?
I was looking for the recommended way to iterate through SCAN using Lua and wondered if there is a "best practice" to talk about this. The script below was the best I could find so far. Anyone have something to add? Can I iterate using COUNT 1000000000? Wouldn't that block?
local ans, has, cursor = {}, {}, "0";
repeat
local t = redis.call("SCAN", cursor, "MATCH", KEYS[1], "COUNT", 1000000000);
local list = t[2];
for i = 1, #list do
local s = list[i];
if has[s] == nil then has[s] = 1; ans[#ans + 1] = s; end;
end;
cursor = t[1];
until cursor == "0";
return #ans; --or return ans;
Taken from: https://github.com/antirez/redis/issues/3190#issuecomment-214022437
source to share
The script will block while it runs and until the end - in your example, which will happen after SCAN
ning completes . The more COUNT
hints at the command SCAN
, the more results that each call will return, resulting in more RAM needed to store the response (variable t
). This can lead to a lack of memory, so you should refrain from using such large values COUNT
. I recommend sticking with the default (100) and just omitting the hint COUNT
.
source to share