description and source-codeconnect-redis = function (session) {
var Store = session.Store;
/**
* Initialize RedisStore with the given `options`.
*
* @param {Object} options
* @api public
*/
function RedisStore (options) {
if (!(this instanceof RedisStore)) {
throw new TypeError('Cannot call RedisStore constructor as a function');
}
var self = this;
options = options || {};
Store.call(this, options);
this.prefix = options.prefix == null
? 'sess:'
: options.prefix;
delete options.prefix;
this.serializer = options.serializer || JSON;
if (options.url) {
options.socket = options.url;
}
// convert to redis connect params
if (options.client) {
this.client = options.client;
}
else if (options.socket) {
this.client = redis.createClient(options.socket, options);
}
else {
this.client = redis.createClient(options);
}
// logErrors
if(options.logErrors){
// if options.logErrors is function, allow it to override. else provide default logger. useful for large scale deployment
// which may need to write to a distributed log
if(typeof options.logErrors != 'function'){
options.logErrors = function (err) {
console.error('Warning: connect-redis reported a client error: ' + err);
};
}
this.client.on('error', options.logErrors);
}
if (options.pass) {
this.client.auth(options.pass, function (err) {
if (err) {
throw err;
}
});
}
this.ttl = options.ttl;
this.disableTTL = options.disableTTL;
if (options.unref) this.client.unref();
if ('db' in options) {
if (typeof options.db !== 'number') {
console.error('Warning: connect-redis expects a number for the "db" option');
}
self.client.select(options.db);
self.client.on('connect', function () {
self.client.select(options.db);
});
}
self.client.on('error', function (er) {
debug('Redis returned err', er);
self.emit('disconnect', er);
});
self.client.on('connect', function () {
self.emit('connect');
});
}
/**
* Inherit from `Store`.
*/
util.inherits(RedisStore, Store);
/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/
RedisStore.prototype.get = function (sid, fn) {
var store = this;
var psid = store.prefix + sid;
if (!fn) fn = noop;
debug('GET "%s"', sid);
store.client.get(psid, function (er, data) {
if (er) return fn(er);
if (!data) return fn();
var result;
data = data.toString();
debug('GOT %s', data);
try {
result = store.serializer.parse(data);
}
catch (er) {
return fn(er);
}
return fn(null, result);
});
};
/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/
RedisStore.prototype.set = function (sid, sess, fn) {
var store = this;
var args = [store.prefix + sid];
if (!fn) fn = noop;
try {
var jsess = store.serializer.stringify(sess);
}
catch (er) {
return fn(er);
}
args.push(jsess);
if (!store.disableTTL) {
var ttl = getTTL(store, sess);
args.push('EX', ttl);
debug('SET "%s" %s ttl:%s', sid, jsess, ttl);
} else {
debug('SET "%s" %s', sid, jsess);
}
store.client.set(args, function (er) {
if (er) return fn(er);
debug('SET complete');
fn.apply(null, arguments);
});
};
/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @api public
*/
RedisStore.prototype.destroy = function (sid, fn) {
debug('DEL "%s"', sid);
if (Array.isArray(sid)) {
var multi = this.client.multi();
var prefix = this.prefix;
sid.forEach(function (s) {
multi.del( ...