more work

This commit is contained in:
Baris Usakli
2013-12-02 16:19:30 -05:00
parent 80e7fd93c6
commit 4f654fb489
5 changed files with 157 additions and 72 deletions

View File

@@ -2,65 +2,76 @@
(function(module) {
'use strict';
var RedisDB,
var redisClient,
redis = require('redis'),
winston = require('winston'),
nconf = require('nconf'),
redis_socket_or_host = nconf.get('redis:host'),
utils = require('./../public/src/utils.js');
utils = require('./../../public/src/utils.js');
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 */
RedisDB = redis.createClient(nconf.get('redis:host'));
redisClient = redis.createClient(nconf.get('redis:host'));
} else {
/* Else, connect over tcp/ip */
RedisDB = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
}
if (nconf.get('redis:password')) {
RedisDB.auth(nconf.get('redis:password'));
redisClient.auth(nconf.get('redis:password'));
}
var db = parseInt(nconf.get('redis:database'), 10);
if (db){
RedisDB.select(db, function(error){
if(error !== null){
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();
}
});
}
RedisDB.handle = function(error) {
if (error !== null) {
winston.err(error);
if (global.env !== 'production') {
throw new Error(error);
}
}
};
/*
* A possibly more efficient way of doing multiple sismember calls
*/
RedisDB.sismembers = function(key, needles, callback) {
function sismembers(key, needles, callback) {
var tempkey = key + ':temp:' + utils.generateUUID();
RedisDB.sadd(tempkey, needles, function() {
RedisDB.sinter(key, tempkey, function(err, data) {
RedisDB.del(tempkey);
redisClient.sadd(tempkey, needles, function() {
redisClient.sinter(key, tempkey, function(err, data) {
redisClient.del(tempkey);
callback(err, data);
});
});
};
/*
* gets fields of a hash as an object instead of an array
*/
RedisDB.hmgetObject = function(key, fields, callback) {
RedisDB.hmget(key, fields, function(err, data) {
//
// Exported functions
//
module.setObject = function(key, data, callback) {
redisClient.hmset(key, data, callback);
}
module.setObjectField = function(key, field, callback) {
redisClient.hset(key, field, callback)
}
module.getObject = function(key, callback) {
redisClient.hgetall(key, callback)
}
module.getObjectField = function(key, field, callback) {
module.getObjectFields(key, [field], function(err, data) {
if(err) {
return callback(err);
}
callback(null, data[field]);
});
}
module.getObjectFields = function(key, fields, callback) {
redisClient.hmget(key, fields, function(err, data) {
if(err) {
return callback(err, null);
}
@@ -73,9 +84,18 @@
callback(null, returnData);
});
};
}
module.deleteObjectField = function(key, field, callback) {
redisClient.hdel(key, field, callback);
}
module.incrObjectField = function(key, field, value, callback) {
redisClient.hincrby(key, field, value, callback);
}
module.exports = RedisDB;
}(exports));