fix: update follower/following counts after removing uid from zsets

use batch instead of async.each
This commit is contained in:
Barış Soner Uşaklı
2024-06-19 10:15:31 -04:00
parent b98333f38c
commit 6f6cfb1a5a

View File

@@ -209,18 +209,22 @@ module.exports = function (User) {
]);
async function updateCount(uids, name, fieldName) {
await async.each(uids, async (uid) => {
let count = await db.sortedSetCard(name + uid);
count = parseInt(count, 10) || 0;
await db.setObjectField(`user:${uid}`, fieldName, count);
await batch.processArray(uids, async (uids) => {
const counts = await db.sortedSetsCard(uids.map(uid => name + uid));
const bulkSet = counts.map(
(count, index) => ([`user:${uids[index]}`, { [fieldName]: count || 0 }])
);
await db.setObjectBulk(bulkSet);
}, {
batch: 500,
});
}
const followingSets = followers.map(uid => `following:${uid}`);
const followerSets = following.map(uid => `followers:${uid}`);
await db.sortedSetsRemove(followerSets.concat(followingSets), uid);
await Promise.all([
db.sortedSetsRemove(followerSets.concat(followingSets), uid),
updateCount(following, 'followers:', 'followerCount'),
updateCount(followers, 'following:', 'followingCount'),
]);