You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.4 KiB
JavaScript

11 years ago
11 years ago
(function(module) {
'use strict';
11 years ago
var redisClient,
11 years ago
redis = require('redis'),
winston = require('winston'),
nconf = require('nconf'),
redis_socket_or_host = nconf.get('redis:host'),
11 years ago
utils = require('./../../public/src/utils.js');
11 years ago
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 */
11 years ago
redisClient = redis.createClient(nconf.get('redis:host'));
11 years ago
} else {
/* Else, connect over tcp/ip */
11 years ago
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
11 years ago
}
if (nconf.get('redis:password')) {
11 years ago
redisClient.auth(nconf.get('redis:password'));
11 years ago
}
var db = parseInt(nconf.get('redis:database'), 10);
if (db){
11 years ago
redisClient.select(db, function(error) {
if(error) {
11 years ago
winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message);
process.exit();
}
});
}
/*
* A possibly more efficient way of doing multiple sismember calls
*/
11 years ago
function sismembers(key, needles, callback) {
11 years ago
var tempkey = key + ':temp:' + utils.generateUUID();
11 years ago
redisClient.sadd(tempkey, needles, function() {
redisClient.sinter(key, tempkey, function(err, data) {
redisClient.del(tempkey);
11 years ago
callback(err, data);
});
});
};
11 years ago
//
// Exported functions
//
module.setObject = function(key, data, callback) {
redisClient.hmset(key, data, callback);
}
11 years ago
11 years ago
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) {
11 years ago
if(err) {
return callback(err, null);
}
var returnData = {};
for (var i = 0, ii = fields.length; i < ii; ++i) {
returnData[fields[i]] = data[i];
}
callback(null, returnData);
});
11 years ago
}
module.deleteObjectField = function(key, field, callback) {
redisClient.hdel(key, field, callback);
}
module.incrObjectField = function(key, field, value, callback) {
redisClient.hincrby(key, field, value, callback);
}
11 years ago
}(exports));
11 years ago