From 9627e539228dda238512ab365831a17b78f046f2 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 10 Dec 2016 00:25:49 +0300 Subject: [PATCH] change group membership methods --- src/groups/membership.js | 102 ++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 54 deletions(-) diff --git a/src/groups/membership.js b/src/groups/membership.js index d03dba3e59..e57847a61c 100644 --- a/src/groups/membership.js +++ b/src/groups/membership.js @@ -336,91 +336,85 @@ module.exports = function (Groups) { return process.nextTick(callback, null, cache.get(cacheKey)); } - db.isSortedSetMember('group:' + groupName + ':members', uid, function (err, isMember) { - if (err) { - return callback(err); + async.waterfall([ + function (next) { + db.isSortedSetMember('group:' + groupName + ':members', uid, next); + }, + function (isMember, next) { + cache.set(cacheKey, isMember); + next(null, isMember); } - - cache.set(cacheKey, isMember); - callback(null, isMember); - }); + ], callback); }; Groups.isMembers = function (uids, groupName, callback) { + function getFromCache(next) { + process.nextTick(next, null, uids.map(function (uid) { + return cache.get(uid + ':' + groupName); + })); + } + if (!groupName || !uids.length) { return callback(null, uids.map(function () {return false;})); } - var nonCachedUids = []; - uids.forEach(function (uid) { - if (!cache.has(uid + ':' + groupName)) { - nonCachedUids.push(uid); - } + var nonCachedUids = uids.filter(function (uid) { + return !cache.has(uid + ':' + groupName); }); if (!nonCachedUids.length) { - var result = uids.map(function (uid) { - return cache.get(uid + ':' + groupName); - }); - return process.nextTick(callback, null, result); + return getFromCache(callback); } - db.isSortedSetMembers('group:' + groupName + ':members', nonCachedUids, function (err, isMembers) { - if (err) { - return callback(err); - } - - nonCachedUids.forEach(function (uid, index) { - cache.set(uid + ':' + groupName, isMembers[index]); - }); - - var result = uids.map(function (uid) { - return cache.get(uid + ':' + groupName); - }); + async.waterfall([ + function (next) { + db.isSortedSetMembers('group:' + groupName + ':members', nonCachedUids, next); + }, + function (isMembers, next) { + nonCachedUids.forEach(function (uid, index) { + cache.set(uid + ':' + groupName, isMembers[index]); + }); - callback(null, result); - }); + getFromCache(next); + } + ], callback); }; Groups.isMemberOfGroups = function (uid, groups, callback) { + function getFromCache(next) { + process.nextTick(next, null, groups.map(function (groupName) { + return cache.get(uid + ':' + groupName); + })); + } + if (!uid || parseInt(uid, 10) <= 0 || !groups.length) { return callback(null, groups.map(function () {return false;})); } - var nonCachedGroups = []; - - groups.forEach(function (groupName) { - if (!cache.has(uid + ':' + groupName)) { - nonCachedGroups.push(groupName); - } + var nonCachedGroups = groups.filter(function (groupName) { + return !cache.has(uid + ':' + groupName); }); - // are they all cached? if (!nonCachedGroups.length) { - var result = groups.map(function (groupName) { - return cache.get(uid + ':' + groupName); - }); - return process.nextTick(callback, null, result); + return getFromCache(callback); } var nonCachedGroupsMemberSets = nonCachedGroups.map(function (groupName) { return 'group:' + groupName + ':members'; }); - db.isMemberOfSortedSets(nonCachedGroupsMemberSets, uid, function (err, isMembers) { - if (err) { - return callback(err); - } - - nonCachedGroups.forEach(function (groupName, index) { - cache.set(uid + ':' + groupName, isMembers[index]); - }); + async.waterfall([ + function (next) { + db.isMemberOfSortedSets(nonCachedGroupsMemberSets, uid, next); + }, + function (isMembers, next) { + nonCachedGroups.forEach(function (groupName, index) { + cache.set(uid + ':' + groupName, isMembers[index]); + }); - var result = groups.map(function (groupName) { - return cache.get(uid + ':' + groupName); - }); - callback(null, result); - }); + getFromCache(next); + } + ], callback); }; Groups.getMemberCount = function (groupName, callback) {