3
我一直在尋找推薦的方式來遍歷使用Lua的掃描,並想知道是否有一個「最佳做法」可以說。下面的腳本是迄今爲止我能找到的最好的腳本。有沒有人有任何補充?迭代使用COUNT 1000000000是否明智?那不會被阻擋?在Lua中爲Redis迭代掃描的推薦方式是什麼?
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;
來自https://github.com/antirez/redis/issues/3190#issuecomment-214022437
因此,該腳本是原子和阻塞,所以唯一的擔心是,COUNT可能會耗盡大量的RAM。謝謝伊塔馬爾。 – geoyws