Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

(#778) perf: add checking and loading scripts uniqueness in pipeline #781

Merged
merged 1 commit into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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