Created
January 29, 2016 07:28
-
-
Save xizhibei/41fa0ed4b355fd95fc1c to your computer and use it in GitHub Desktop.
DB connection
This file contains 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
import nconf from 'nconf'; | |
import mongoose from 'mongoose'; | |
import Promise from 'bluebird'; | |
import logger from './winston'; | |
import _s from 'underscore.string'; | |
import models from '../models/index'; | |
mongoose.Promise = Promise; | |
mongoose.connect(nconf.get('mongo'), function () { | |
logger.info('MongoDB connect success'); | |
}); | |
mongoose.connection.on('error', function () { | |
logger.error('MongoDB Connection Error. Make sure MongoDB is running.'); | |
}); | |
export default mongoose; |
This file contains 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
import _ from 'underscore'; | |
import redis from 'redis'; | |
import nconf from 'nconf'; | |
import Promise from 'bluebird'; | |
import logger from '../config/winston'; | |
Promise.promisifyAll(redis.RedisClient.prototype); | |
Promise.promisifyAll(redis.Multi.prototype); | |
const redisConfig = nconf.get('redis'); | |
const clientCache = {}; | |
export function getRedisClient(dbName) { | |
const config = redisConfig[dbName]; | |
let db = clientCache[dbName]; | |
if (!db) { | |
db = redis.createClient(config.port, config.host); | |
db.setMaxListeners(0); | |
if (config.index) { | |
db.select(config.index); | |
} | |
clientCache[dbName] = db; | |
logger.info('Connect to redis success', 'db#' + config.index, 'name:' + dbName); | |
} | |
return db; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment