From 6f6cfb1a5ab37546641dbab885d052ef94f79df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 19 Jun 2024 10:15:31 -0400 Subject: [PATCH] fix: update follower/following counts after removing uid from zsets use batch instead of async.each --- src/user/delete.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/user/delete.js b/src/user/delete.js index 9efd8802ae..b84b4ef1d8 100644 --- a/src/user/delete.js +++ b/src/user/delete.js @@ -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'), ]);