change group membership methods

v1.18.x
barisusakli 8 years ago
parent 4db171985d
commit 9627e53922

@ -336,91 +336,85 @@ module.exports = function (Groups) {
return process.nextTick(callback, null, cache.get(cacheKey)); return process.nextTick(callback, null, cache.get(cacheKey));
} }
db.isSortedSetMember('group:' + groupName + ':members', uid, function (err, isMember) { async.waterfall([
if (err) { function (next) {
return callback(err); db.isSortedSetMember('group:' + groupName + ':members', uid, next);
} },
function (isMember, next) {
cache.set(cacheKey, isMember); cache.set(cacheKey, isMember);
callback(null, isMember); next(null, isMember);
}); }
], callback);
}; };
Groups.isMembers = function (uids, groupName, 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) { if (!groupName || !uids.length) {
return callback(null, uids.map(function () {return false;})); return callback(null, uids.map(function () {return false;}));
} }
var nonCachedUids = []; var nonCachedUids = uids.filter(function (uid) {
uids.forEach(function (uid) { return !cache.has(uid + ':' + groupName);
if (!cache.has(uid + ':' + groupName)) {
nonCachedUids.push(uid);
}
}); });
if (!nonCachedUids.length) { if (!nonCachedUids.length) {
var result = uids.map(function (uid) { return getFromCache(callback);
return cache.get(uid + ':' + groupName);
});
return process.nextTick(callback, null, result);
}
db.isSortedSetMembers('group:' + groupName + ':members', nonCachedUids, function (err, isMembers) {
if (err) {
return callback(err);
} }
async.waterfall([
function (next) {
db.isSortedSetMembers('group:' + groupName + ':members', nonCachedUids, next);
},
function (isMembers, next) {
nonCachedUids.forEach(function (uid, index) { nonCachedUids.forEach(function (uid, index) {
cache.set(uid + ':' + groupName, isMembers[index]); cache.set(uid + ':' + groupName, isMembers[index]);
}); });
var result = uids.map(function (uid) { getFromCache(next);
return cache.get(uid + ':' + groupName); }
}); ], callback);
callback(null, result);
});
}; };
Groups.isMemberOfGroups = function (uid, groups, 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) { if (!uid || parseInt(uid, 10) <= 0 || !groups.length) {
return callback(null, groups.map(function () {return false;})); return callback(null, groups.map(function () {return false;}));
} }
var nonCachedGroups = []; var nonCachedGroups = groups.filter(function (groupName) {
return !cache.has(uid + ':' + groupName);
groups.forEach(function (groupName) {
if (!cache.has(uid + ':' + groupName)) {
nonCachedGroups.push(groupName);
}
}); });
// are they all cached?
if (!nonCachedGroups.length) { if (!nonCachedGroups.length) {
var result = groups.map(function (groupName) { return getFromCache(callback);
return cache.get(uid + ':' + groupName);
});
return process.nextTick(callback, null, result);
} }
var nonCachedGroupsMemberSets = nonCachedGroups.map(function (groupName) { var nonCachedGroupsMemberSets = nonCachedGroups.map(function (groupName) {
return 'group:' + groupName + ':members'; return 'group:' + groupName + ':members';
}); });
db.isMemberOfSortedSets(nonCachedGroupsMemberSets, uid, function (err, isMembers) { async.waterfall([
if (err) { function (next) {
return callback(err); db.isMemberOfSortedSets(nonCachedGroupsMemberSets, uid, next);
} },
function (isMembers, next) {
nonCachedGroups.forEach(function (groupName, index) { nonCachedGroups.forEach(function (groupName, index) {
cache.set(uid + ':' + groupName, isMembers[index]); cache.set(uid + ':' + groupName, isMembers[index]);
}); });
var result = groups.map(function (groupName) { getFromCache(next);
return cache.get(uid + ':' + groupName); }
}); ], callback);
callback(null, result);
});
}; };
Groups.getMemberCount = function (groupName, callback) { Groups.getMemberCount = function (groupName, callback) {

Loading…
Cancel
Save