Files
NodeBB/src/database/redis.js

144 lines
3.5 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'),
2014-07-02 14:07:08 -04:00
session = require('express-session'),
2013-12-09 14:16:04 -05:00
utils = require('./../../public/src/utils.js'),
redis,
connectRedis,
2015-03-14 23:00:24 -04:00
redisSearch,
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,
before: function(value) { value = value || nconf.get('redis:password') || ''; return value; }
},
{
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');
2014-07-02 15:15:06 -04:00
connectRedis = require('connect-redis')(session);
2015-03-14 23:00:24 -04:00
redisSearch = require('redisearch');
} catch (err) {
winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message);
process.exit();
}
redisClient = module.connect();
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
});
2015-03-14 23:00:24 -04:00
module.postSearch = redisSearch.createSearch('nodebbpostsearch', redisClient);
module.topicSearch = redisSearch.createSearch('nodebbtopicsearch', redisClient);
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.connect = function(options) {
var redis_socket_or_host = nconf.get('redis:host'),
cxn, dbIdx;
options = options || {};
if (!redis) {
redis = require('redis');
}
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 */
cxn = redis.createClient(nconf.get('redis:host'), options);
} else {
/* Else, connect over tcp/ip */
cxn = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'), options);
2014-09-25 17:13:09 -04:00
}
2014-11-20 19:02:29 -05:00
cxn.on('error', function (err) {
winston.error(err.stack);
process.exit(1);
});
2014-09-25 17:13:09 -04:00
if (nconf.get('redis:password')) {
cxn.auth(nconf.get('redis:password'));
}
dbIdx = parseInt(nconf.get('redis:database'), 10);
if (dbIdx) {
cxn.select(dbIdx, function(error) {
if(error) {
winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message);
process.exit();
}
});
}
return cxn;
};
2014-04-14 13:51:45 -04:00
module.close = function() {
redisClient.quit();
};
2014-04-22 01:39:57 -04:00
module.info = function(cxn, callback) {
cxn.info(function (err, data) {
if (err) {
return callback(err);
}
var lines = data.toString().split("\r\n").sort();
var redisData = {};
lines.forEach(function (line) {
var parts = line.split(':');
if (parts[1]) {
redisData[parts[0]] = parts[1];
}
});
redisData.raw = JSON.stringify(redisData, null, 4);
redisData.redis = true;
callback(null, redisData);
});
};
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