diff --git a/lib/pipeline.js b/lib/pipeline.js index 29a14779..a36a1867 100644 --- a/lib/pipeline.js +++ b/lib/pipeline.js @@ -266,19 +266,19 @@ Pipeline.prototype.exec = function (callback) { return execPipeline(); } - return this.redis.script('exists', scripts.map(function (item) { - return item.sha; - })).then(function (results) { - var pending = []; - for (var i = 0; i < results.length; ++i) { - if (!results[i]) { - pending.push(scripts[i]); + return this.redis + .script('exists', Array.from(new Set(scripts.map(({ sha }) => sha)))) + .then(function (results) { + var pending = []; + for (var i = 0; i < results.length; ++i) { + if (!results[i]) { + pending.push(scripts[i]); + } } - } - var Promise = PromiseContainer.get() - return Promise.all(pending.map(function (script) { - return _this.redis.script('load', script.lua); - })); + var Promise = PromiseContainer.get() + return Promise.all(pending.map(function (script) { + return _this.redis.script('load', script.lua); + })); }).then(execPipeline); function execPipeline() { diff --git a/test/functional/pipeline.js b/test/functional/pipeline.js index b06a480a..e9e1d0d4 100644 --- a/test/functional/pipeline.js +++ b/test/functional/pipeline.js @@ -222,6 +222,35 @@ describe('pipeline', function () { }); }); }); + + it('should check and load uniq scripts only', function (done) { + var redis = new Redis(); + redis.defineCommand('test', { + numberOfKeys: 1, + lua: 'return {unpack(KEYS),unpack(ARGV)}' + }); + redis.defineCommand('echo', { + numberOfKeys: 1, + lua: 'return {KEYS[1],ARGV[1]}' + }); + + redis.once('ready', function () { + var expectedComands = ['script', 'script', 'script', 'evalsha', 'evalsha', 'evalsha', 'evalsha']; + redis.monitor(function (err, monitor) { + monitor.on('monitor', function (_, command) { + var name = expectedComands.shift(); + expect(name).to.eql(command[0]); + if (!expectedComands.length) { + monitor.disconnect(); + redis.disconnect(); + done(); + } + }); + var pipe = redis.pipeline(); + pipe.echo('f', '0').test('a', '1').echo('b', '2').test('b', '3').exec(); + }); + }); + }); }); describe('#length', function () {