Skip to content

Commit

Permalink
perf: add checking and loading scripts uniqueness in pipeline (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
akaNightmare authored and luin committed Jan 13, 2019
1 parent 580094d commit 66075ba
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
29 changes: 29 additions & 0 deletions test/functional/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 66075ba

Please sign in to comment.