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.

429 lines
9.7 KiB
JavaScript

11 years ago
'use strict';
11 years ago
11 years ago
(function(module) {
11 years ago
11 years ago
var winston = require('winston'),
11 years ago
nconf = require('nconf'),
11 years ago
path = require('path'),
express = require('express'),
11 years ago
redis_socket_or_host = nconf.get('redis:host'),
11 years ago
utils = require('./../../public/src/utils.js'),
redis,
connectRedis,
reds,
11 years ago
redisClient,
postSearch,
topicSearch;
11 years ago
try {
redis = require('redis');
connectRedis = require('connect-redis')(express);
reds = require('reds');
11 years ago
} catch (err) {
11 years ago
winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message);
11 years ago
process.exit();
}
11 years ago
11 years ago
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'));
}
11 years ago
if (nconf.get('redis:password')) {
redisClient.auth(nconf.get('redis:password'));
} else {
winston.warn('You have no redis password setup!');
}
11 years ago
11 years ago
redisClient.on('error', function (err) {
winston.error(err.message);
process.exit();
});
11 years ago
11 years ago
module.client = redisClient;
11 years ago
module.sessionStore = new connectRedis({
client: redisClient,
ttl: 60 * 60 * 24 * 14
});
11 years ago
reds.createClient = function () {
return reds.client || (reds.client = redisClient);
};
postSearch = reds.createSearch('nodebbpostsearch');
topicSearch = reds.createSearch('nodebbtopicsearch');
11 years ago
var db = parseInt(nconf.get('redis:database'), 10);
11 years ago
11 years ago
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();
}
});
}
11 years ago
if(typeof callback === 'function') {
callback();
}
11 years ago
};
module.close = function() {
redisClient.quit();
11 years ago
};
11 years ago
//
// Exported functions
//
module.searchIndex = function(key, content, id) {
11 years ago
if (key === 'post') {
postSearch.index(content, id);
} else if(key === 'topic') {
topicSearch.index(content, id);
}
11 years ago
};
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);
}
11 years ago
};
module.searchRemove = function(key, id, callback) {
if(key === 'post') {
postSearch.remove(id);
} else if(key === 'topic') {
topicSearch.remove(id);
}
if (typeof callback === 'function') {
11 years ago
callback();
}
11 years ago
};
module.flushdb = function(callback) {
redisClient.send_command('flushdb', [], function(err) {
11 years ago
if (typeof callback === 'function') {
callback(err);
}
});
11 years ago
};
11 years ago
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];
11 years ago
}
});
11 years ago
redisData.raw = JSON.stringify(redisData, null, 4);
redisData.redis = true;
callback(null, redisData);
11 years ago
});
11 years ago
};
11 years ago
11 years ago
// key
module.exists = function(key, callback) {
redisClient.exists(key, function(err, exists) {
callback(err, exists === 1);
});
11 years ago
};
11 years ago
module.delete = function(key, callback) {
redisClient.del(key, callback);
11 years ago
};
11 years ago
11 years ago
module.get = function(key, callback) {
redisClient.get(key, callback);
11 years ago
};
11 years ago
module.set = function(key, value, callback) {
redisClient.set(key, value, callback);
11 years ago
};
11 years ago
11 years ago
module.rename = function(oldKey, newKey, callback) {
redisClient.rename(oldKey, newKey, callback);
11 years ago
};
11 years ago
module.expire = function(key, seconds, callback) {
redisClient.expire(key, seconds, callback);
11 years ago
};
module.expireAt = function(key, timestamp, callback) {
redisClient.expireat(key, timestamp, callback);
11 years ago
};
11 years ago
//hashes
11 years ago
module.setObject = function(key, data, callback) {
11 years ago
// TODO: this crashes if callback isnt supplied -baris
redisClient.hmset(key, data, function(err, res) {
11 years ago
if(typeof callback === 'function') {
11 years ago
callback(err, res);
}
});
11 years ago
};
11 years ago
11 years ago
module.setObjectField = function(key, field, value, callback) {
11 years ago
redisClient.hset(key, field, value, callback);
11 years ago
};
11 years ago
module.getObject = function(key, callback) {
11 years ago
redisClient.hgetall(key, callback);
11 years ago
};
11 years ago
module.getObjects = function(keys, callback) {
var multi = redisClient.multi();
for(var x=0; x<keys.length; ++x) {
multi.hgetall(keys[x]);
}
11 years ago
multi.exec(callback);
11 years ago
};
11 years ago
module.getObjectField = function(key, field, callback) {
module.getObjectFields(key, [field], function(err, data) {
if(err) {
return callback(err);
}
callback(null, data[field]);
});
11 years ago
};
11 years ago
module.getObjectFields = function(key, fields, callback) {
module.getObjectsFields([key], fields, function(err, results) {
callback(err, results ? results[0]: null);
});
};
module.getObjectsFields = function(keys, fields, callback) {
var multi = redisClient.multi();
for(var x=0; x<keys.length; ++x) {
multi.hmget.apply(multi, [keys[x]].concat(fields));
}
11 years ago
function makeObject(array) {
var obj = {};
11 years ago
for (var i = 0, ii = fields.length; i < ii; ++i) {
obj[fields[i]] = array[i];
}
return obj;
}
multi.exec(function(err, results) {
if (err) {
return callback(err);
11 years ago
}
results = results.map(makeObject);
callback(null, results);
11 years ago
});
11 years ago
};
11 years ago
module.getObjectKeys = function(key, callback) {
redisClient.hkeys(key, callback);
11 years ago
};
11 years ago
module.getObjectValues = function(key, callback) {
redisClient.hvals(key, callback);
11 years ago
};
11 years ago
module.isObjectField = function(key, field, callback) {
redisClient.hexists(key, field, function(err, exists) {
callback(err, exists === 1);
});
11 years ago
};
11 years ago
11 years ago
module.deleteObjectField = function(key, field, callback) {
redisClient.hdel(key, field, callback);
11 years ago
};
11 years ago
11 years ago
module.incrObjectField = function(key, field, callback) {
redisClient.hincrby(key, field, 1, callback);
11 years ago
};
11 years ago
11 years ago
module.decrObjectField = function(key, field, callback) {
redisClient.hincrby(key, field, -1, callback);
11 years ago
};
11 years ago
11 years ago
module.incrObjectFieldBy = function(key, field, value, callback) {
redisClient.hincrby(key, field, value, callback);
11 years ago
};
11 years ago
11 years ago
// sets
module.setAdd = function(key, value, callback) {
redisClient.sadd(key, value, callback);
11 years ago
};
module.setRemove = function(key, value, callback) {
redisClient.srem(key, value, callback);
11 years ago
};
module.isSetMember = function(key, value, callback) {
redisClient.sismember(key, value, function(err, result) {
if(err) {
return callback(err);
}
callback(null, result === 1);
});
11 years ago
};
module.isSetMembers = function(key, values, callback) {
var multi = redisClient.multi();
for (var i=0; i<values.length; ++i) {
multi.sismember(key, values[i]);
}
multi.exec(function(err, results) {
if (err) {
return callback(err);
}
for (var i=0; i<results.length; ++i) {
results[i] = results[i] === 1;
}
callback(null, results);
});
};
11 years ago
module.isMemberOfSets = function(sets, value, callback) {
var multi = redisClient.multi();
11 years ago
for (var i = 0, ii = sets.length; i < ii; i++) {
multi.sismember(sets[i], value);
11 years ago
}
multi.exec(callback);
11 years ago
};
11 years ago
module.getSetMembers = function(key, callback) {
redisClient.smembers(key, callback);
11 years ago
};
11 years ago
11 years ago
module.setCount = function(key, callback) {
redisClient.scard(key, callback);
11 years ago
};
11 years ago
11 years ago
module.setRemoveRandom = function(key, callback) {
redisClient.spop(key, callback);
11 years ago
};
11 years ago
11 years ago
// sorted sets
11 years ago
module.sortedSetAdd = function(key, score, value, callback) {
redisClient.zadd(key, score, value, callback);
11 years ago
};
11 years ago
module.sortedSetRemove = function(key, value, callback) {
redisClient.zrem(key, value, callback);
11 years ago
};
11 years ago
11 years ago
module.getSortedSetRange = function(key, start, stop, callback) {
11 years ago
redisClient.zrange(key, start, stop, callback);
11 years ago
};
11 years ago
module.getSortedSetRevRange = function(key, start, stop, callback) {
11 years ago
redisClient.zrevrange(key, start, stop, callback);
11 years ago
};
11 years ago
module.getSortedSetRangeByScore = function(args, callback) {
redisClient.zrangebyscore(args, callback);
};
11 years ago
module.getSortedSetRevRangeByScore = function(args, callback) {
redisClient.zrevrangebyscore(args, callback);
11 years ago
};
11 years ago
module.sortedSetCount = function(key, min, max, callback) {
redisClient.zcount(key, min, max, callback);
11 years ago
};
11 years ago
module.sortedSetCard = function(key, callback) {
redisClient.zcard(key, callback);
11 years ago
};
module.sortedSetRank = function(key, value, callback) {
redisClient.zrank(key, value, callback);
11 years ago
};
11 years ago
module.sortedSetRevRank = function(key, value, callback) {
redisClient.zrevrank(key, value, callback);
11 years ago
};
11 years ago
module.sortedSetScore = function(key, value, callback) {
redisClient.zscore(key, value, callback);
11 years ago
};
module.isSortedSetMember = function(key, value, callback) {
module.sortedSetScore(key, value, function(err, score) {
callback(err, !!score);
});
11 years ago
};
module.sortedSetsScore = function(keys, value, callback) {
var multi = redisClient.multi();
for(var x=0; x<keys.length; ++x) {
multi.zscore(keys[x], value);
}
multi.exec(callback);
11 years ago
};
11 years ago
// lists
module.listPrepend = function(key, value, callback) {
redisClient.lpush(key, value, callback);
11 years ago
};
11 years ago
11 years ago
module.listAppend = function(key, value, callback) {
redisClient.rpush(key, value, callback);
11 years ago
};
11 years ago
module.listRemoveLast = function(key, callback) {
redisClient.rpop(key, callback);
11 years ago
};
module.listRemoveAll = function(key, value, callback) {
redisClient.lrem(key, 0, value, callback);
11 years ago
};
11 years ago
module.getListRange = function(key, start, stop, callback) {
redisClient.lrange(key, start, stop, callback);
11 years ago
};
11 years ago
}(exports));
11 years ago