Skip to content

Commit 66075ba

Browse files
akaNightmareluin
authored andcommitted
perf: add checking and loading scripts uniqueness in pipeline (#781)
1 parent 580094d commit 66075ba

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

lib/pipeline.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,19 @@ Pipeline.prototype.exec = function (callback) {
266266
return execPipeline();
267267
}
268268

269-
return this.redis.script('exists', scripts.map(function (item) {
270-
return item.sha;
271-
})).then(function (results) {
272-
var pending = [];
273-
for (var i = 0; i < results.length; ++i) {
274-
if (!results[i]) {
275-
pending.push(scripts[i]);
269+
return this.redis
270+
.script('exists', Array.from(new Set(scripts.map(({ sha }) => sha))))
271+
.then(function (results) {
272+
var pending = [];
273+
for (var i = 0; i < results.length; ++i) {
274+
if (!results[i]) {
275+
pending.push(scripts[i]);
276+
}
276277
}
277-
}
278-
var Promise = PromiseContainer.get()
279-
return Promise.all(pending.map(function (script) {
280-
return _this.redis.script('load', script.lua);
281-
}));
278+
var Promise = PromiseContainer.get()
279+
return Promise.all(pending.map(function (script) {
280+
return _this.redis.script('load', script.lua);
281+
}));
282282
}).then(execPipeline);
283283

284284
function execPipeline() {

test/functional/pipeline.js

+29
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,35 @@ describe('pipeline', function () {
222222
});
223223
});
224224
});
225+
226+
it('should check and load uniq scripts only', function (done) {
227+
var redis = new Redis();
228+
redis.defineCommand('test', {
229+
numberOfKeys: 1,
230+
lua: 'return {unpack(KEYS),unpack(ARGV)}'
231+
});
232+
redis.defineCommand('echo', {
233+
numberOfKeys: 1,
234+
lua: 'return {KEYS[1],ARGV[1]}'
235+
});
236+
237+
redis.once('ready', function () {
238+
var expectedComands = ['script', 'script', 'script', 'evalsha', 'evalsha', 'evalsha', 'evalsha'];
239+
redis.monitor(function (err, monitor) {
240+
monitor.on('monitor', function (_, command) {
241+
var name = expectedComands.shift();
242+
expect(name).to.eql(command[0]);
243+
if (!expectedComands.length) {
244+
monitor.disconnect();
245+
redis.disconnect();
246+
done();
247+
}
248+
});
249+
var pipe = redis.pipeline();
250+
pipe.echo('f', '0').test('a', '1').echo('b', '2').test('b', '3').exec();
251+
});
252+
});
253+
});
225254
});
226255

227256
describe('#length', function () {

0 commit comments

Comments
 (0)