Skip to content

Commit

Permalink
feat: add dropBufferSupport option to improve the performance
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed May 3, 2016
1 parent bffd4b9 commit cb05340
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
27 changes: 25 additions & 2 deletions lib/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
var _ = require('lodash');
var Command = require('./command');
var Script = require('./script');
var Promise = require('bluebird');

var DROP_BUFFER_SUPPORT_ERROR = '*Buffer methods are not available ' +
'because "dropBufferSupport" option is enabled.' +
'Refer to /~https://github.com/luin/ioredis/wiki/Improve-Performance for more details.';

/**
* Commander
Expand Down Expand Up @@ -106,7 +111,16 @@ function generateFunction(_commandName, _encoding) {
args[i - firstArgIndex] = arguments[i];
}

var options = { replyEncoding: _encoding };
var options;
if (this.options.dropBufferSupport) {
if (!_encoding) {
return Promise.reject(new Error(DROP_BUFFER_SUPPORT_ERROR)).nodeify(callback);
}
options = { replyEncoding: null };
} else {
options = { replyEncoding: _encoding };
}

if (this.options.showFriendlyErrorStack) {
options.errorStack = new Error().stack;
}
Expand All @@ -133,7 +147,16 @@ function generateScriptingFunction(_script, _encoding) {
args[i] = arguments[i];
}

var options = { replyEncoding: _encoding };
var options;
if (this.options.dropBufferSupport) {
if (!_encoding) {
return Promise.reject(new Error(DROP_BUFFER_SUPPORT_ERROR)).nodeify(callback);
}
options = { replyEncoding: null };
} else {
options = { replyEncoding: _encoding };
}

if (this.options.showFriendlyErrorStack) {
options.errorStack = new Error().stack;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var ScanStream = require('./scan_stream');
* @param {number} [options.db=0] - Database index to use.
* @param {string} [options.password=null] - If set, client will send AUTH command
* with the value of this option when connected.
* @param {string} [options.parser=null] - Either "hiredis" or "javascript". If not set, "hiredis" parser
* will be used if it's installed (`npm install hiredis`), otherwise "javascript" parser will be used.
* @param {boolean} [options.dropBufferSupport=false] - Drop the buffer support for better performance.
* This option is recommanded to be enabled when "hiredis" parser is used.
* Refer to /~https://github.com/luin/ioredis/wiki/Improve-Performance for more details.
* @param {boolean} [options.enableReadyCheck=true] - When a connection is established to
* the Redis server, the server might still be loading the database from disk.
* While loading, the server not respond to any commands.
Expand Down Expand Up @@ -166,6 +171,7 @@ Redis.defaultOptions = {
db: 0,
// Others
parser: null,
dropBufferSupport: false,
enableOfflineQueue: true,
enableReadyCheck: true,
autoResubscribe: true,
Expand Down
8 changes: 7 additions & 1 deletion lib/redis/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.initParser = function () {
this.replyParser = new Parser({
name: this.options.parser,
stringNumbers: this.options.stringNumbers,
returnBuffers: true,
returnBuffers: !this.options.dropBufferSupport,
returnError: function (err) {
_this.returnError(new ReplyError(err.message));
},
Expand All @@ -33,6 +33,12 @@ exports.initParser = function () {
_this.disconnect(true);
}
});

if (this.replyParser.name === 'hiredis' && !this.options.dropBufferSupport) {
console.warn('ioredis is using hiredis parser, however "dropBufferSupport" is disabled. ' +
'It\'s highly recommanded to enable this option. ' +
'Refer to /~https://github.com/luin/ioredis/wiki/Improve-Performance for more details.');
}
};

exports.returnError = function (err) {
Expand Down

0 comments on commit cb05340

Please sign in to comment.