Created
July 16, 2012 22:39
-
-
Save distracteddev/3125546 to your computer and use it in GitHub Desktop.
Connect to a redisToGo instance (Heroku or Nodejitsu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To get a Redis URL provided by Nodejitsu run the following in your terminal: | |
// $ jitsu databases create redis [name_of_database] | |
// | |
// This will output a message with your new Redis' instance URL. Copy/Paste it below: | |
var REDIS_URL = process.env.REDISTOGO_URL // REPLACE THIS WITH YOUR URL PROVIDED BY THE JITSU CLI | |
var Resourceful = require('resourceful-redis') | |
, url = require('url'); | |
// A function helper to connect to redis using Heroku's redis url | |
var connect = function(redis_url) { | |
var password, database; | |
var parsed_url = url.parse(redis_url || process.env.REDISTOGO_URL || REDIS_URL || 'redis://localhost:6379'); | |
var parsed_auth = (parsed_url.auth || '').split(':'); | |
var redis = require('redis').createClient(parsed_url.port, parsed_url.hostname); | |
if (password = parsed_auth[1]) { | |
redis.auth(password, function(err) { | |
if (err) throw err; | |
}); | |
} | |
// Select the right database | |
if (database = parsed_auth[0]) { | |
redis.select(database); | |
redis.on('connect', function() { | |
redis.send_anyways = true | |
redis.select(database); | |
redis.send_anyways = false; | |
}); | |
} | |
return(redis); | |
}; | |
// Get a new redis connection | |
var redisConnection = exports.redisConnection = connect(REDIS_URL); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment