sortedSetsAdd

using mullti/bulk for redis/mongo
lets you add a score/value pair to multiple sorted sets at once
v1.18.x
barisusakli 11 years ago
parent f16ced6bcc
commit 5222edb624

@ -20,6 +20,14 @@ module.exports = function(db, module) {
}); });
}; };
module.sortedSetsAdd = function(keys, score, value, callback) {
async.each(keys, function(key, next) {
module.sortedSetAdd(key, score, value, next);
}, function(err) {
callback(err);
});
};
module.sortedSetRemove = function(key, value, callback) { module.sortedSetRemove = function(key, value, callback) {
module.getListRange(key, 0, -1, function(err, set) { module.getListRange(key, 0, -1, function(err, set) {
set = set.filter(function(a) {return a.value !== value.toString();}); set = set.filter(function(a) {return a.value !== value.toString();});

@ -18,6 +18,26 @@ module.exports = function(db, module) {
}); });
}; };
module.sortedSetsAdd = function(keys, score, value, callback) {
callback = callback || helpers.noop;
value = helpers.valueToString(value);
var data = {
score: parseInt(score, 10),
value: value
};
var bulk = db.collection('objects').initializeUnorderedBulkOp();
for(var i=0; i<keys.length; ++i) {
bulk.find({_key: keys[i], value: value}).upsert().updateOne({$set: data});
}
bulk.execute(function(err, result) {
callback(err);
});
};
module.sortedSetRemove = function(key, value, callback) { module.sortedSetRemove = function(key, value, callback) {
callback = callback || helpers.noop; callback = callback || helpers.noop;
value = helpers.valueToString(value); value = helpers.valueToString(value);
@ -34,6 +54,7 @@ module.exports = function(db, module) {
db.collection('objects').remove({_key: {$in: keys}, value: value}, callback); db.collection('objects').remove({_key: {$in: keys}, value: value}, callback);
}; };
function getSortedSetRange(key, start, stop, sort, withScores, callback) { function getSortedSetRange(key, start, stop, sort, withScores, callback) {
db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}}) db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}})
.limit(stop - start + 1) .limit(stop - start + 1)

@ -8,6 +8,19 @@ module.exports = function(redisClient, module) {
}); });
}; };
module.sortedSetsAdd = function(keys, score, value, callback) {
callback = callback || function() {};
var multi = redisClient.multi();
for(var i=0; i<keys.length; ++i) {
multi.zadd(keys[i], score, value);
}
multi.exec(function(err, res) {
callback(err);
});
};
module.sortedSetRemove = function(key, value, callback) { module.sortedSetRemove = function(key, value, callback) {
callback = callback || function() {}; callback = callback || function() {};
redisClient.zrem(key, value, function(err) { redisClient.zrem(key, value, function(err) {

Loading…
Cancel
Save