Skip to content

Instantly share code, notes, and snippets.

@clintandrewhall
Created July 3, 2011 23:43
Show Gist options
  • Save clintandrewhall/1062714 to your computer and use it in GitHub Desktop.
Save clintandrewhall/1062714 to your computer and use it in GitHub Desktop.
Attempting to monitor existence of Redis in Connect/Express
// This just seems silly, honestly... and I'm concerned about my use of app.use
// over and over. :-/
// This var is only necessary b/c the connection to redis continues to be attempted
// if the error event is handled.
var redisConnected = false;
function redisConnect() {
if(!redisConnected) {
logger.warn("Redis Session store connected; applying session.");
app.use(express.session({
"secret" : config.sessionSecret,
"store" : store,
"maxAge" : 1800000
}));
redisConnected = true;
}
}
function redisError(error) {
if(redisConnected) {
logger.error("Redis Session store failed; creating cookie-based session: " + error);
app.use(express.session({
"secret" : config.sessionSecret,
"maxAge" : 1800000
}));
redisConnected = false;
}
}
var RedisStore = require('connect-redis')(express),
store = new RedisStore;
store.client.on("error", redisError);
store.client.on("connect", redisConnect);
app.use(express.session({
"secret" : config.sessionSecret,
"store" : store,
"maxAge" : 1800000
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment