From 7b9a2caec361570d79c07162225aaabec6ea4ad0 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 23 Nov 2018 14:39:59 -0500 Subject: [PATCH] closes #6994 --- src/database/redis/hash.js | 12 +++++------ src/database/redis/helpers.js | 24 +++++++++++----------- src/database/redis/main.js | 8 ++++---- src/database/redis/sets.js | 19 +++++++++-------- src/database/redis/sorted.js | 32 ++++++++++++++--------------- src/database/redis/sorted/add.js | 6 +++--- src/database/redis/sorted/remove.js | 16 +++++++-------- 7 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/database/redis/hash.js b/src/database/redis/hash.js index d1f0477621..5be88c025b 100644 --- a/src/database/redis/hash.js +++ b/src/database/redis/hash.js @@ -75,7 +75,7 @@ module.exports = function (redisClient, module) { async.waterfall([ function (next) { if (unCachedKeys.length > 1) { - helpers.multiKeys(redisClient, 'hgetall', unCachedKeys, next); + helpers.execKeys(redisClient, 'batch', 'hgetall', unCachedKeys, next); } else { redisClient.hgetall(unCachedKeys[0], (err, data) => next(err, [data])); } @@ -139,7 +139,7 @@ module.exports = function (redisClient, module) { }; module.isObjectFields = function (key, fields, callback) { - helpers.multiKeyValues(redisClient, 'hexists', key, fields, function (err, results) { + helpers.execKeyValues(redisClient, 'batch', 'hexists', key, fields, function (err, results) { callback(err, Array.isArray(results) ? helpers.resultsToBool(results) : null); }); }; @@ -156,7 +156,7 @@ module.exports = function (redisClient, module) { }; module.deleteObjectFields = function (key, fields, callback) { - helpers.multiKeyValues(redisClient, 'hdel', key, fields, function (err) { + helpers.execKeyValues(redisClient, 'batch', 'hdel', key, fields, function (err) { cache.delObjectCache(key); callback(err); }); @@ -184,11 +184,11 @@ module.exports = function (redisClient, module) { return callback(null, null); } if (Array.isArray(key)) { - var multi = redisClient.multi(); + var batch = redisClient.batch(); key.forEach(function (key) { - multi.hincrby(key, field, value); + batch.hincrby(key, field, value); }); - multi.exec(done); + batch.exec(done); } else { redisClient.hincrby(key, field, value, done); } diff --git a/src/database/redis/helpers.js b/src/database/redis/helpers.js index bcf2b63e1c..fb1b46909e 100644 --- a/src/database/redis/helpers.js +++ b/src/database/redis/helpers.js @@ -4,31 +4,31 @@ var helpers = module.exports; helpers.noop = function () {}; -helpers.multiKeys = function (redisClient, command, keys, callback) { +helpers.execKeys = function (redisClient, type, command, keys, callback) { callback = callback || function () {}; - var multi = redisClient.multi(); + var queue = redisClient[type](); for (var i = 0; i < keys.length; i += 1) { - multi[command](keys[i]); + queue[command](keys[i]); } - multi.exec(callback); + queue.exec(callback); }; -helpers.multiKeysValue = function (redisClient, command, keys, value, callback) { +helpers.execKeysValue = function (redisClient, type, command, keys, value, callback) { callback = callback || function () {}; - var multi = redisClient.multi(); + var queue = redisClient[type](); for (var i = 0; i < keys.length; i += 1) { - multi[command](String(keys[i]), String(value)); + queue[command](String(keys[i]), String(value)); } - multi.exec(callback); + queue.exec(callback); }; -helpers.multiKeyValues = function (redisClient, command, key, values, callback) { +helpers.execKeyValues = function (redisClient, type, command, key, values, callback) { callback = callback || function () {}; - var multi = redisClient.multi(); + var queue = redisClient[type](); for (var i = 0; i < values.length; i += 1) { - multi[command](String(key), String(values[i])); + queue[command](String(key), String(values[i])); } - multi.exec(callback); + queue.exec(callback); }; helpers.resultsToBool = function (results) { diff --git a/src/database/redis/main.js b/src/database/redis/main.js index 9c93560ee0..c81f3aa73e 100644 --- a/src/database/redis/main.js +++ b/src/database/redis/main.js @@ -23,7 +23,7 @@ module.exports = function (redisClient, module) { module.exists = function (key, callback) { if (Array.isArray(key)) { - helpers.multiKeys(redisClient, 'exists', key, function (err, data) { + helpers.execKeys(redisClient, 'batch', 'exists', key, function (err, data) { callback(err, data && data.map(exists => exists === 1)); }); } else { @@ -43,11 +43,11 @@ module.exports = function (redisClient, module) { module.deleteAll = function (keys, callback) { callback = callback || function () {}; - var multi = redisClient.multi(); + var batch = redisClient.batch(); for (var i = 0; i < keys.length; i += 1) { - multi.del(keys[i]); + batch.del(keys[i]); } - multi.exec(function (err) { + batch.exec(function (err) { module.objectCache.delObjectCache(keys); callback(err); }); diff --git a/src/database/redis/sets.js b/src/database/redis/sets.js index 1da6f4ff22..084cc612b3 100644 --- a/src/database/redis/sets.js +++ b/src/database/redis/sets.js @@ -18,7 +18,7 @@ module.exports = function (redisClient, module) { module.setsAdd = function (keys, value, callback) { callback = callback || function () {}; - helpers.multiKeysValue(redisClient, 'sadd', keys, value, function (err) { + helpers.execKeysValue(redisClient, 'batch', 'sadd', keys, value, function (err) { callback(err); }); }; @@ -31,18 +31,19 @@ module.exports = function (redisClient, module) { if (!Array.isArray(key)) { key = [key]; } - var multi = redisClient.multi(); + + var batch = redisClient.batch(); key.forEach(function (key) { - multi.srem(key, value); + batch.srem(key, value); }); - multi.exec(function (err) { + batch.exec(function (err) { callback(err); }); }; module.setsRemove = function (keys, value, callback) { callback = callback || function () {}; - helpers.multiKeysValue(redisClient, 'srem', keys, value, function (err) { + helpers.execKeysValue(redisClient, 'batch', 'srem', keys, value, function (err) { callback(err); }); }; @@ -54,13 +55,13 @@ module.exports = function (redisClient, module) { }; module.isSetMembers = function (key, values, callback) { - helpers.multiKeyValues(redisClient, 'sismember', key, values, function (err, results) { + helpers.execKeyValues(redisClient, 'batch', 'sismember', key, values, function (err, results) { callback(err, results ? helpers.resultsToBool(results) : null); }); }; module.isMemberOfSets = function (sets, value, callback) { - helpers.multiKeysValue(redisClient, 'sismember', sets, value, function (err, results) { + helpers.execKeysValue(redisClient, 'batch', 'sismember', sets, value, function (err, results) { callback(err, results ? helpers.resultsToBool(results) : null); }); }; @@ -70,7 +71,7 @@ module.exports = function (redisClient, module) { }; module.getSetsMembers = function (keys, callback) { - helpers.multiKeys(redisClient, 'smembers', keys, callback); + helpers.execKeys(redisClient, 'batch', 'smembers', keys, callback); }; module.setCount = function (key, callback) { @@ -78,7 +79,7 @@ module.exports = function (redisClient, module) { }; module.setsCount = function (keys, callback) { - helpers.multiKeys(redisClient, 'scard', keys, callback); + helpers.execKeys(redisClient, 'batch', 'scard', keys, callback); }; module.setRemoveRandom = function (key, callback) { diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js index a38185f41d..9454a3adb5 100644 --- a/src/database/redis/sorted.js +++ b/src/database/redis/sorted.js @@ -92,11 +92,11 @@ module.exports = function (redisClient, module) { if (Array.isArray(keys) && !keys.length) { return callback(null, []); } - var multi = redisClient.multi(); + var batch = redisClient.batch(); for (var i = 0; i < keys.length; i += 1) { - multi.zcard(keys[i]); + batch.zcard(keys[i]); } - multi.exec(callback); + batch.exec(callback); }; module.sortedSetRank = function (key, value, callback) { @@ -104,19 +104,19 @@ module.exports = function (redisClient, module) { }; module.sortedSetsRanks = function (keys, values, callback) { - var multi = redisClient.multi(); + var batch = redisClient.batch(); for (var i = 0; i < values.length; i += 1) { - multi.zrank(keys[i], values[i]); + batch.zrank(keys[i], values[i]); } - multi.exec(callback); + batch.exec(callback); }; module.sortedSetRanks = function (key, values, callback) { - var multi = redisClient.multi(); + var batch = redisClient.batch(); for (var i = 0; i < values.length; i += 1) { - multi.zrank(key, values[i]); + batch.zrank(key, values[i]); } - multi.exec(callback); + batch.exec(callback); }; module.sortedSetRevRank = function (key, value, callback) { @@ -140,11 +140,11 @@ module.exports = function (redisClient, module) { }; module.sortedSetsScore = function (keys, value, callback) { - helpers.multiKeysValue(redisClient, 'zscore', keys, value, callback); + helpers.execKeysValue(redisClient, 'batch', 'zscore', keys, value, callback); }; module.sortedSetScores = function (key, values, callback) { - helpers.multiKeyValues(redisClient, 'zscore', key, values, callback); + helpers.execKeyValues(redisClient, 'batch', 'zscore', key, values, callback); }; module.isSortedSetMember = function (key, value, callback) { @@ -154,7 +154,7 @@ module.exports = function (redisClient, module) { }; module.isSortedSetMembers = function (key, values, callback) { - helpers.multiKeyValues(redisClient, 'zscore', key, values, function (err, results) { + helpers.execKeyValues(redisClient, 'batch', 'zscore', key, values, function (err, results) { if (err) { return callback(err); } @@ -163,7 +163,7 @@ module.exports = function (redisClient, module) { }; module.isMemberOfSortedSets = function (keys, value, callback) { - helpers.multiKeysValue(redisClient, 'zscore', keys, value, function (err, results) { + helpers.execKeysValue(redisClient, 'batch', 'zscore', keys, value, function (err, results) { if (err) { return callback(err); } @@ -175,11 +175,11 @@ module.exports = function (redisClient, module) { if (!Array.isArray(keys) || !keys.length) { return setImmediate(callback, null, []); } - var multi = redisClient.multi(); + var batch = redisClient.batch(); for (var i = 0; i < keys.length; i += 1) { - multi.zrange(keys[i], 0, -1); + batch.zrange(keys[i], 0, -1); } - multi.exec(callback); + batch.exec(callback); }; module.sortedSetIncrBy = function (key, increment, value, callback) { diff --git a/src/database/redis/sorted/add.js b/src/database/redis/sorted/add.js index b279f5ba76..e6f5861704 100644 --- a/src/database/redis/sorted/add.js +++ b/src/database/redis/sorted/add.js @@ -39,15 +39,15 @@ module.exports = function (redisClient, module) { if (!Array.isArray(keys) || !keys.length) { return callback(); } - var multi = redisClient.multi(); + var batch = redisClient.batch(); for (var i = 0; i < keys.length; i += 1) { if (keys[i]) { - multi.zadd(keys[i], score, value); + batch.zadd(keys[i], score, value); } } - multi.exec(function (err) { + batch.exec(function (err) { callback(err); }); }; diff --git a/src/database/redis/sorted/remove.js b/src/database/redis/sorted/remove.js index 886bd7cdb9..cbe2247b3e 100644 --- a/src/database/redis/sorted/remove.js +++ b/src/database/redis/sorted/remove.js @@ -14,33 +14,33 @@ module.exports = function (redisClient, module) { } if (Array.isArray(key)) { - var multi = redisClient.multi(); + var batch = redisClient.batch(); key.forEach(function (key) { - multi.zrem(key, value); + batch.zrem(key, value); }); - multi.exec(function (err) { + batch.exec(function (err) { callback(err); }); } else { - helpers.multiKeyValues(redisClient, 'zrem', key, value, function (err) { + helpers.execKeyValues(redisClient, 'batch', 'zrem', key, value, function (err) { callback(err); }); } }; module.sortedSetsRemove = function (keys, value, callback) { - helpers.multiKeysValue(redisClient, 'zrem', keys, value, function (err) { + helpers.execKeysValue(redisClient, 'batch', 'zrem', keys, value, function (err) { callback(err); }); }; module.sortedSetsRemoveRangeByScore = function (keys, min, max, callback) { callback = callback || function () {}; - var multi = redisClient.multi(); + var batch = redisClient.batch(); for (var i = 0; i < keys.length; i += 1) { - multi.zremrangebyscore(keys[i], min, max); + batch.zremrangebyscore(keys[i], min, max); } - multi.exec(function (err) { + batch.exec(function (err) { callback(err); }); };