feat: allow passing min,max to sortedSetsCardSum

to get rid of multiple db calls in profile page
This commit is contained in:
Barış Soner Uşaklı
2024-06-07 19:14:13 -04:00
parent 6bbe3d1c4c
commit 70b4a0e2ae
5 changed files with 99 additions and 71 deletions

View File

@@ -116,16 +116,21 @@ module.exports = function (module) {
return await helpers.execBatch(batch);
};
module.sortedSetsCardSum = async function (keys) {
module.sortedSetsCardSum = async function (keys, min = '-inf', max = '+inf') {
if (!keys || (Array.isArray(keys) && !keys.length)) {
return 0;
}
if (!Array.isArray(keys)) {
keys = [keys];
}
const counts = await module.sortedSetsCard(keys);
const sum = counts.reduce((acc, val) => acc + val, 0);
return sum;
const batch = module.client.batch();
if (min !== '-inf' || max !== '+inf') {
keys.forEach(k => batch.zcount(String(k), min, max));
} else {
keys.forEach(k => batch.zcard(String(k)));
}
const counts = await helpers.execBatch(batch);
return counts.reduce((acc, val) => acc + val, 0);
};
module.sortedSetRank = async function (key, value) {