fix: handle scan/zscan returning duplicate elements on redis

increase count on redis zscan
v1.18.x
Barış Soner Uşaklı 5 years ago
parent ee38e05daa
commit 746222d6c6

@ -26,11 +26,19 @@ module.exports = function (module) {
module.scan = async function (params) {
let cursor = '0';
let returnData = [];
const seen = {};
do {
/* eslint-disable no-await-in-loop */
const res = await module.client.async.scan(cursor, 'MATCH', params.match, 'COUNT', 10000);
cursor = res[0];
returnData = returnData.concat(res[1]);
const values = res[1].filter((value) => {
const isSeen = !!seen[value];
if (!isSeen) {
seen[value] = 1;
}
return !isSeen;
});
returnData = returnData.concat(values);
} while (cursor !== '0');
return returnData;
};

@ -282,24 +282,28 @@ module.exports = function (module) {
const returnData = [];
let done = false;
const seen = {};
do {
/* eslint-disable no-await-in-loop */
const res = await module.client.async.zscan(params.key, cursor, 'MATCH', params.match, 'COUNT', 100);
const res = await module.client.async.zscan(params.key, cursor, 'MATCH', params.match, 'COUNT', 5000);
cursor = res[0];
done = cursor === '0';
const data = res[1];
for (let i = 0; i < data.length; i += 2) {
const value = data[i];
const score = parseFloat(data[i + 1]);
if (params.withScores) {
returnData.push({ value: value, score: score });
} else {
returnData.push(value);
}
if (params.limit && returnData.length >= params.limit) {
done = true;
break;
if (!seen[value]) {
seen[value] = 1;
if (params.withScores) {
returnData.push({ value: value, score: parseFloat(data[i + 2]) });
} else {
returnData.push(value);
}
if (params.limit && returnData.length >= params.limit) {
done = true;
break;
}
}
}
} while (!done);

Loading…
Cancel
Save