Reputation: 3697
I've been looking around for the recommended way to iterate over SCANs using Lua and was wondering if there was a "best practice" so to speak for it. The script below was the best I could find so far. Does anyone have anything to add? Is it wise to iterate using COUNT 1000000000? Wouldn't that be blocking?
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
Upvotes: 3
Views: 2628
Reputation: 50112
The script will block while it is running and until it ends - in your example that will happen once SCAN
ning is complete. The bigger the COUNT
hint to the SCAN
command, the more results each call to it will return, resulting in more RAM needed for storing the reply (the variable t
). This could lead to an out of memory condition, so you should refrain from using such large COUNT
values. I recommend sticking with the default (100), and just omitting the COUNT
hint.
Upvotes: 1