Files
NodeBB/src/database/redis.js

114 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-03-01 15:45:43 -05:00
'use strict';
2013-12-02 15:45:15 -05:00
2013-12-02 15:46:25 -05:00
(function(module) {
2014-03-01 15:45:43 -05:00
2013-12-09 14:16:04 -05:00
var winston = require('winston'),
2013-12-02 15:46:25 -05:00
nconf = require('nconf'),
2014-03-01 15:45:43 -05:00
path = require('path'),
express = require('express'),
2013-12-02 15:46:25 -05:00
redis_socket_or_host = nconf.get('redis:host'),
2013-12-09 14:16:04 -05:00
utils = require('./../../public/src/utils.js'),
redis,
connectRedis,
reds,
2014-03-01 16:59:04 -05:00
redisClient,
postSearch,
topicSearch;
2013-12-09 14:16:04 -05:00
module.questions = [
{
name: 'redis:host',
description: 'Host IP or address of your Redis instance',
'default': nconf.get('redis:host') || '127.0.0.1'
},
{
name: 'redis:port',
description: 'Host port of your Redis instance',
'default': nconf.get('redis:port') || 6379
},
{
name: 'redis:password',
description: 'Password of your Redis database',
hidden: true
},
{
name: "redis:database",
description: "Which database to use (0..n)",
'default': nconf.get('redis:database') || 0
}
];
2013-12-02 15:46:25 -05:00
2014-03-01 16:59:04 -05:00
module.init = function(callback) {
try {
redis = require('redis');
connectRedis = require('connect-redis')(express);
reds = require('reds');
} catch (err) {
winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message);
process.exit();
}
2014-03-01 16:59:04 -05:00
if (redis_socket_or_host && redis_socket_or_host.indexOf('/')>=0) {
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
redisClient = redis.createClient(nconf.get('redis:host'));
} else {
/* Else, connect over tcp/ip */
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
}
2014-03-01 16:59:04 -05:00
if (nconf.get('redis:password')) {
redisClient.auth(nconf.get('redis:password'));
} else {
winston.warn('You have no redis password setup!');
}
2013-12-23 19:17:03 -05:00
2014-03-01 16:59:04 -05:00
redisClient.on('error', function (err) {
winston.error(err.message);
process.exit();
});
2013-12-23 19:17:03 -05:00
2014-03-01 16:59:04 -05:00
module.client = redisClient;
2014-03-01 16:59:04 -05:00
module.sessionStore = new connectRedis({
client: redisClient,
ttl: 60 * 60 * 24 * 14
});
2014-03-01 16:59:04 -05:00
reds.createClient = function () {
return reds.client || (reds.client = redisClient);
};
2014-04-11 15:29:01 -04:00
module.postSearch = reds.createSearch('nodebbpostsearch');
module.topicSearch = reds.createSearch('nodebbtopicsearch');
2014-03-01 16:59:04 -05:00
var db = parseInt(nconf.get('redis:database'), 10);
2013-12-02 15:46:25 -05:00
2014-03-01 16:59:04 -05:00
if (db) {
redisClient.select(db, function(error) {
if(error) {
winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message);
process.exit();
}
});
}
2013-12-02 15:46:25 -05:00
2014-04-11 15:29:01 -04:00
require('./redis/main')(redisClient, module);
require('./redis/hash')(redisClient, module);
require('./redis/sets')(redisClient, module);
require('./redis/sorted')(redisClient, module);
require('./redis/list')(redisClient, module);
2014-04-11 15:29:01 -04:00
if(typeof callback === 'function') {
2014-03-01 15:45:43 -05:00
callback();
}
2014-03-01 15:45:43 -05:00
};
2014-04-14 13:51:45 -04:00
module.close = function() {
redisClient.quit();
};
module.helpers = module.helpers || {};
2014-04-15 12:02:36 -04:00
module.helpers.redis = require('./redis/helpers');
2013-12-02 15:46:25 -05:00
}(exports));
2013-12-02 15:45:15 -05:00