(function(module) { 'use strict'; var winston = require('winston'), nconf = require('nconf'), express = require('express'), redis_socket_or_host = nconf.get('redis:host'), utils = require('./../../public/src/utils.js'), redis, connectRedis, reds, redisClient; 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(); } 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')); } module.client = redisClient; module.sessionStore = new connectRedis({ client: redisClient, ttl: 60 * 60 * 24 * 30 }); reds.createClient = function () { return reds.client || (reds.client = redisClient); }; var userSearch = reds.createSearch('nodebbusersearch'), postSearch = reds.createSearch('nodebbpostsearch'), topicSearch = reds.createSearch('nodebbtopicsearch'); if (nconf.get('redis:password')) { redisClient.auth(nconf.get('redis:password')); } var db = parseInt(nconf.get('redis:database'), 10); 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(); } }); } module.init = function(callback) { callback(null); } /* * A possibly more efficient way of doing multiple sismember calls */ function sismembers(key, needles, callback) { var tempkey = key + ':temp:' + utils.generateUUID(); redisClient.sadd(tempkey, needles, function() { redisClient.sinter(key, tempkey, function(err, data) { redisClient.del(tempkey); callback(err, data); }); }); }; // // Exported functions // module.searchIndex = function(key, content, id) { if(key === 'post') { postSearch.index(content, id); } else if(key === 'topic') { topicSearch.index(content, id); } else if(key === 'user') { userSearch.index(content, id); } } module.search = function(key, term, callback) { function search(searchObj, callback) { searchObj .query(term).type('or') .end(callback); } if(key === 'post') { search(postSearch, callback); } else if(key === 'topic') { search(topicSearch, callback); } else if(key === 'user') { search(userSearch, callback); } } module.searchRemove = function(key, id) { if(key === 'post') { postSearch.remove(id); } else if(key === 'topic') { topicSearch.remove(id); } else if(key === 'user') { userSearch.remove(id); } } module.flushdb = function(callback) { redisClient.send_command('flushdb', [], function(err) { if(err){ winston.error(error); return callback(err); } callback(null); }); } module.getFileName = function(callback) { var multi = redisClient.multi(); multi.config('get', 'dir'); multi.config('get', 'dbfilename'); multi.exec(function (err, results) { if (err) { return callback(err); } results = results.reduce(function (memo, config) { memo[config[0]] = config[1]; return memo; }, {}); var dbFile = path.join(results.dir, results.dbfilename); callback(null, dbFile); }); } module.info = function(callback) { redisClient.info(function (err, data) { if(err) { return callback(err); } data = data.split("\r\n"); var redisData = {}; for (var i in data) { if (data[i].indexOf(':') == -1 || !data[i]) continue; try { data[i] = data[i].replace(/:/, "\":\""); var json = "{\"" + data[i] + "\"}"; var jsonObject = JSON.parse(json); for (var key in jsonObject) { redisData[key] = jsonObject[key]; } } catch (err) { winston.warn('can\'t parse redis status variable, ignoring', i, data[i], err); } } redisData.raw = JSON.stringify(redisData, null, 4); redisData.redis = true; callback(null, redisData); }); } // key module.exists = function(key, callback) { redisClient.exists(key, function(err, exists) { callback(err, exists === 1); }); } module.delete = function(key, callback) { redisClient.del(key, callback); } module.get = function(key, callback) { redisClient.get(key, callback); } module.set = function(key, value, callback) { redisClient.set(key, value, callback); } module.keys = function(key, callback) { redisClient.keys(key, callback); } //hashes module.setObject = function(key, data, callback) { // TODO: this crashes if callback isnt supplied -baris redisClient.hmset(key, data, function(err, res) { if(callback) { callback(err, res); } }); } module.setObjectField = function(key, field, value, callback) { redisClient.hset(key, field, value, callback); } module.getObject = function(key, callback) { redisClient.hgetall(key, callback); } module.getObjects = function(keys, callback) { var multi = redisClient.multi(); for(var x=0; x