'use strict'; (function(module) { var winston = require('winston'), nconf = require('nconf'), path = require('path'), express = require('express'), redis_socket_or_host = nconf.get('redis:host'), utils = require('./../../public/src/utils.js'), redis, connectRedis, reds, redisClient, postSearch, topicSearch; 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(); } module.init = function(callback) { 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')); } if (nconf.get('redis:password')) { redisClient.auth(nconf.get('redis:password')); } else { winston.warn('You have no redis password setup!'); } redisClient.on('error', function (err) { winston.error(err.message); process.exit(); }); module.client = redisClient; module.sessionStore = new connectRedis({ client: redisClient, ttl: 60 * 60 * 24 * 14 }); reds.createClient = function () { return reds.client || (reds.client = redisClient); }; postSearch = reds.createSearch('nodebbpostsearch'); topicSearch = reds.createSearch('nodebbtopicsearch'); 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(); } }); } if(typeof callback === 'function') { callback(); } }; module.close = function() { redisClient.quit(); }; // // Exported functions // module.searchIndex = function(key, content, id) { if (key === 'post') { postSearch.index(content, id); } else if(key === 'topic') { topicSearch.index(content, id); } }; module.search = function(key, term, limit, callback) { function search(searchObj, callback) { searchObj .query(term) .between(0, limit - 1) .type('or') .end(callback); } if(key === 'post') { search(postSearch, callback); } else if(key === 'topic') { search(topicSearch, callback); } }; module.searchRemove = function(key, id, callback) { if(key === 'post') { postSearch.remove(id); } else if(key === 'topic') { topicSearch.remove(id); } if (typeof callback === 'function') { callback(); } }; module.flushdb = function(callback) { redisClient.send_command('flushdb', [], function(err) { if (typeof callback === 'function') { callback(err); } }); }; module.info = function(callback) { redisClient.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); }); }; // 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); }; module.rename = function(oldKey, newKey, callback) { redisClient.rename(oldKey, newKey, callback); }; module.expire = function(key, seconds, callback) { redisClient.expire(key, seconds, callback); }; module.expireAt = function(key, timestamp, callback) { redisClient.expireat(key, timestamp, 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