description and source-codekoa-redis = function (options) {
if (!(this instanceof RedisStore)) {
return new RedisStore(options);
}
EventEmitter.call(this);
options = options || {};
var client;
options.auth_pass = options.auth_pass || options.pass || null; // For backwards compatibility
options.path = options.path || options.socket || null; // For backwards compatibility
if (!options.client) {
debug('Init redis new client');
client = redis.createClient(options);
} else {
if (options.duplicate) { // Duplicate client and update with options provided
debug('Duplicating provided client with new options (if provided)');
var dupClient = options.client;
delete options.client;
delete options.duplicate;
client = dupClient.duplicate(options); // Useful if you want to use the DB option without adjusting
the client DB outside koa-redis
} else {
debug('Using provided client');
client = options.client;
}
}
if (options.db) {
debug('selecting db %s', options.db)
client.select(options.db);
client.on('connect', function() {
client.send_anyways = true;
client.select(options.db);
client.send_anyways = false;
});
}
client.on('error', this.emit.bind(this, 'error'));
client.on('end', this.emit.bind(this, 'end'));
client.on('end', this.emit.bind(this, 'disconnect')); // For backwards compatibility
client.on('connect', this.emit.bind(this, 'connect'));
client.on('reconnecting', this.emit.bind(this, 'reconnecting'));
client.on('ready', this.emit.bind(this, 'ready'));
client.on('warning', this.emit.bind(this, 'warning'));
this.on('connect', function() {
debug('connected to redis');
this.connected = client.connected;
});
this.on('ready', function() {
debug('redis ready');
});
this.on('end', function() {
debug('redis ended');
this.connected = client.connected;
});
// No good way to test error
this.on('error', function() {
debug('redis error');
this.connected = client.connected;
});
// No good way to test reconnect
/* istanbul ignore next */
this.on('reconnecting', function() {
debug('redis reconnecting');
this.connected = client.connected;
});
// No good way to test warning
/* istanbul ignore next */
this.on('warning', function() {
debug('redis warning');
this.connected = client.connected;
});
//wrap redis
this._redisClient = client;
this.client = redisWrapper(client);
this.connected = client.connected;
}