redis helpers

v1.18.x
barisusakli 10 years ago
parent 0bf98b5494
commit 2c45dc6450

@ -1,6 +1,9 @@
"use strict";
module.exports = function(redisClient, module) {
var helpers = module.helpers.redis;
module.setObject = function(key, data, callback) {
callback = callback || function() {};
redisClient.hmset(key, data, function(err) {
@ -20,28 +23,18 @@ module.exports = function(redisClient, module) {
};
module.getObjects = function(keys, callback) {
var multi = redisClient.multi();
for(var x=0; x<keys.length; ++x) {
multi.hgetall(keys[x]);
}
multi.exec(callback);
helpers.multiKeys(redisClient, 'hgetall', keys, callback);
};
module.getObjectField = function(key, field, callback) {
module.getObjectFields(key, [field], function(err, data) {
if(err) {
return callback(err);
}
callback(null, data[field]);
callback(err, data ? data[field] : null);
});
};
module.getObjectFields = function(key, fields, callback) {
module.getObjectsFields([key], fields, function(err, results) {
callback(err, results ? results[0]: null);
callback(err, results ? results[0] : null);
});
};
@ -89,20 +82,8 @@ module.exports = function(redisClient, module) {
};
module.isObjectFields = function(key, fields, callback) {
var multi = redisClient.multi();
for (var i=0; i<fields.length; ++i) {
multi.hexists(key, fields[i]);
}
multi.exec(function(err, results) {
if (err) {
return callback(err);
}
for (var i=0; i<results.length; ++i) {
results[i] = results[i] === 1;
}
callback(null, results);
helpers.multiKeyValues(redisClient, 'hexists', key, fields, function(err, results) {
callback(err, Array.isArray(results) ? helpers.resultsToBool(results) : null);
});
};

@ -2,5 +2,38 @@
var helpers = {};
helpers.multiKeys = function(redisClient, command, keys, callback) {
callback = callback || function() {};
var multi = redisClient.multi();
for (var i=0; i<keys.length; ++i) {
multi[command](keys[i]);
}
multi.exec(callback);
};
helpers.multiKeysValue = function(redisClient, command, keys, value, callback) {
callback = callback || function() {};
var multi = redisClient.multi();
for (var i=0; i<keys.length; ++i) {
multi[command](keys[i], value);
}
multi.exec(callback);
};
helpers.multiKeyValues = function(redisClient, command, key, values, callback) {
callback = callback || function() {};
var multi = redisClient.multi();
for (var i=0; i<values.length; ++i) {
multi[command](key, values[i]);
}
multi.exec(callback);
};
helpers.resultsToBool = function(results) {
for (var i=0; i<results.length; ++i) {
results[i] = results[i] === 1;
}
return results;
}
module.exports = helpers;

@ -1,6 +1,8 @@
"use strict";
module.exports = function(redisClient, module) {
var helpers = module.helpers.redis;
module.setAdd = function(key, value, callback) {
callback = callback || function() {};
redisClient.sadd(key, value, function(err, res) {
@ -9,12 +11,7 @@ module.exports = function(redisClient, module) {
};
module.setsAdd = function(keys, value, callback) {
callback = callback || function() {};
var multi = redisClient.multi();
for (var i=0; i<keys.length; ++i) {
multi.sadd(keys[i], value);
}
multi.exec(function(err, res) {
helpers.multiKeysValue(redisClient, 'sadd', keys, value, function(err, res) {
callback(err);
});
};
@ -28,67 +25,35 @@ module.exports = function(redisClient, module) {
module.setsRemove = function(keys, value, callback) {
callback = callback || function() {};
var multi = redisClient.multi();
for(var i=0; i<keys.length; ++i) {
multi.srem(keys[i], value);
}
multi.exec(function(err, res) {
helpers.multiKeysValue(redisClient, 'srem', keys, value, function(err, res) {
callback(err);
});
};
module.isSetMember = function(key, value, callback) {
redisClient.sismember(key, value, function(err, result) {
if(err) {
return callback(err);
}
callback(null, result === 1);
callback(err, result === 1);
});
};
module.isSetMembers = function(key, values, callback) {
var multi = redisClient.multi();
for (var i=0; i<values.length; ++i) {
multi.sismember(key, values[i]);
}
execSetMembers(multi, callback);
helpers.multiKeyValues(redisClient, 'sismember', key, values, function(err, results) {
callback(err, results ? helpers.resultsToBool(results) : null);
});
};
module.isMemberOfSets = function(sets, value, callback) {
var multi = redisClient.multi();
for (var i = 0; i < sets.length; ++i) {
multi.sismember(sets[i], value);
}
execSetMembers(multi, callback);
};
function execSetMembers(multi, callback) {
multi.exec(function(err, results) {
if (err) {
return callback(err);
}
for (var i=0; i<results.length; ++i) {
results[i] = results[i] === 1;
}
callback(null, results);
helpers.multiKeysValue(redisClient, 'sismember', sets, value, function(err, results) {
callback(err, results ? helpers.resultsToBool(results) : null);
});
}
};
module.getSetMembers = function(key, callback) {
redisClient.smembers(key, callback);
};
module.getSetsMembers = function(keys, callback) {
var multi = redisClient.multi();
for (var i=0; i<keys.length; ++i) {
multi.smembers(keys[i]);
}
multi.exec(callback);
helpers.multiKeys(redisClient, 'smembers', keys, callback);
};
module.setCount = function(key, callback) {
@ -96,11 +61,7 @@ module.exports = function(redisClient, module) {
};
module.setsCount = function(keys, callback) {
var multi = redisClient.multi();
for (var i=0; i<keys.length; ++i) {
multi.scard(keys[i]);
}
multi.exec(callback);
helpers.multiKeys(redisClient, 'scard', keys, callback);
};
module.setRemoveRandom = function(key, callback) {

@ -1,6 +1,9 @@
"use strict";
module.exports = function(redisClient, module) {
var helpers = module.helpers.redis;
module.sortedSetAdd = function(key, score, value, callback) {
callback = callback || function() {};
if (Array.isArray(score) && Array.isArray(value)) {
@ -49,17 +52,14 @@ module.exports = function(redisClient, module) {
if (!Array.isArray(value)) {
value = [value];
}
var multi = redisClient.multi();
for(var i=0; i<value.length; ++i) {
multi.zrem(key, value[i]);
}
multi.exec(function(err) {
helpers.multiKeyValues(redisClient, 'zrem', key, value, function(err, result) {
callback(err);
});
};
module.sortedSetsRemove = function(keys, value, callback) {
multi('zrem', keys, value, function(err) {
helpers.multiKeysValue(redisClient, 'zrem', keys, value, function(err, result) {
callback(err);
});
};
@ -70,7 +70,7 @@ module.exports = function(redisClient, module) {
for(var i=0; i<keys.length; ++i) {
multi.zremrangebyscore(keys[i], min, max);
}
multi.exec(function(err) {
multi.exec(function(err, result) {
callback(err);
});
};
@ -181,15 +181,11 @@ module.exports = function(redisClient, module) {
};
module.sortedSetsScore = function(keys, value, callback) {
multi('zscore', keys, value, callback);
helpers.multiKeysValue(redisClient, 'zscore', keys, value, callback);
};
module.sortedSetScores = function(key, values, callback) {
var multi = redisClient.multi();
for(var i=0; i<values.length; ++i) {
multi.zscore(key, values[i]);
}
multi.exec(callback);
helpers.multiKeyValues(redisClient, 'zscore', key, values, callback);
};
module.isSortedSetMember = function(key, value, callback) {
@ -199,34 +195,20 @@ module.exports = function(redisClient, module) {
};
module.isSortedSetMembers = function(key, values, callback) {
var multi = redisClient.multi();
for (var i=0; i<values.length; ++i) {
multi.zscore(key, values[i]);
}
multi.exec(function(err, results) {
helpers.multiKeyValues(redisClient, 'zscore', key, values, function(err, results) {
if (err) {
return callback(err);
}
results = results.map(function(score) {
return !!score;
});
callback(null, results);
callback(null, results.map(Boolean));
});
};
module.isMemberOfSortedSets = function(keys, value, callback) {
var multi = redisClient.multi();
for (var i=0; i<keys.length; ++i) {
multi.zscore(keys[i], value);
}
multi.exec(function(err, results) {
helpers.multiKeysValue(redisClient, 'zscore', keys, value, function(err, results) {
if (err) {
return callback(err);
}
results = results.map(function(score) {
return !!score;
});
callback(null, results);
callback(null, results.map(Boolean));
});
};
@ -235,21 +217,9 @@ module.exports = function(redisClient, module) {
for (var i=0; i<keys.length; ++i) {
multi.zrange(keys[i], 0, -1);
}
multi.exec(function(err, results) {
callback(err, results);
});
multi.exec(callback);
};
function multi(command, keys, value, callback) {
var m = redisClient.multi();
for(var x=0; x<keys.length; ++x) {
m[command](keys[x], value);
}
m.exec(callback);
}
module.getSortedSetUnion = function(sets, start, stop, callback) {
sortedSetUnion(sets, false, start, stop, callback);
};

Loading…
Cancel
Save