hinted redis.js

v1.18.x
Baris Soner Usakli 11 years ago
parent 8eca195530
commit fff3ba5bec

@ -1,9 +1,10 @@
'use strict';
(function(module) { (function(module) {
'use strict';
var winston = require('winston'), var winston = require('winston'),
nconf = require('nconf'), nconf = require('nconf'),
path = require('path'),
express = require('express'), express = require('express'),
redis_socket_or_host = nconf.get('redis:host'), redis_socket_or_host = nconf.get('redis:host'),
utils = require('./../../public/src/utils.js'), utils = require('./../../public/src/utils.js'),
@ -53,13 +54,12 @@
return reds.client || (reds.client = redisClient); return reds.client || (reds.client = redisClient);
}; };
var userSearch = reds.createSearch('nodebbusersearch'), var postSearch = reds.createSearch('nodebbpostsearch'),
postSearch = reds.createSearch('nodebbpostsearch'),
topicSearch = reds.createSearch('nodebbtopicsearch'); topicSearch = reds.createSearch('nodebbtopicsearch');
var db = parseInt(nconf.get('redis:database'), 10); var db = parseInt(nconf.get('redis:database'), 10);
if (db){ if (db) {
redisClient.select(db, function(error) { redisClient.select(db, function(error) {
if(error) { if(error) {
winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message); winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message);
@ -70,24 +70,22 @@
module.init = function(callback) { module.init = function(callback) {
callback(null); callback(null);
} };
module.close = function() { module.close = function() {
redisClient.quit(); redisClient.quit();
} };
// //
// Exported functions // Exported functions
// //
module.searchIndex = function(key, content, id) { module.searchIndex = function(key, content, id) {
if(key === 'post') { if (key === 'post') {
postSearch.index(content, id); postSearch.index(content, id);
} else if(key === 'topic') { } else if(key === 'topic') {
topicSearch.index(content, id); topicSearch.index(content, id);
} else if(key === 'user') {
userSearch.index(content, id);
} }
} };
module.search = function(key, term, limit, callback) { module.search = function(key, term, limit, callback) {
function search(searchObj, callback) { function search(searchObj, callback) {
@ -102,34 +100,30 @@
search(postSearch, callback); search(postSearch, callback);
} else if(key === 'topic') { } else if(key === 'topic') {
search(topicSearch, callback); search(topicSearch, callback);
} else if(key === 'user') {
search(userSearch, callback);
} }
} };
module.searchRemove = function(key, id, callback) { module.searchRemove = function(key, id, callback) {
if(key === 'post') { if(key === 'post') {
postSearch.remove(id); postSearch.remove(id);
} else if(key === 'topic') { } else if(key === 'topic') {
topicSearch.remove(id); topicSearch.remove(id);
} else if(key === 'user') {
userSearch.remove(id);
} }
if (typeof callback === 'function') { if (typeof callback === 'function') {
callback() callback();
} }
} };
module.flushdb = function(callback) { module.flushdb = function(callback) {
redisClient.send_command('flushdb', [], function(err) { redisClient.send_command('flushdb', [], function(err) {
if(err){ if (err) {
winston.error(error); winston.error(err.message);
return callback(err); return callback(err);
} }
callback(null); callback();
}); });
} };
module.getFileName = function(callback) { module.getFileName = function(callback) {
var multi = redisClient.multi(); var multi = redisClient.multi();
@ -149,8 +143,7 @@
var dbFile = path.join(results.dir, results.dbfilename); var dbFile = path.join(results.dir, results.dbfilename);
callback(null, dbFile); callback(null, dbFile);
}); });
} };
module.info = function(callback) { module.info = function(callback) {
redisClient.info(function (err, data) { redisClient.info(function (err, data) {
@ -172,7 +165,7 @@
callback(null, redisData); callback(null, redisData);
}); });
} };
// key // key
@ -180,35 +173,35 @@
redisClient.exists(key, function(err, exists) { redisClient.exists(key, function(err, exists) {
callback(err, exists === 1); callback(err, exists === 1);
}); });
} };
module.delete = function(key, callback) { module.delete = function(key, callback) {
redisClient.del(key, callback); redisClient.del(key, callback);
} };
module.get = function(key, callback) { module.get = function(key, callback) {
redisClient.get(key, callback); redisClient.get(key, callback);
} };
module.set = function(key, value, callback) { module.set = function(key, value, callback) {
redisClient.set(key, value, callback); redisClient.set(key, value, callback);
} };
module.keys = function(key, callback) { module.keys = function(key, callback) {
redisClient.keys(key, callback); redisClient.keys(key, callback);
} };
module.rename = function(oldKey, newKey, callback) { module.rename = function(oldKey, newKey, callback) {
redisClient.rename(oldKey, newKey, callback); redisClient.rename(oldKey, newKey, callback);
} };
module.expire = function(key, seconds, callback) { module.expire = function(key, seconds, callback) {
redisClient.expire(key, seconds, callback); redisClient.expire(key, seconds, callback);
} };
module.expireAt = function(key, timestamp, callback) { module.expireAt = function(key, timestamp, callback) {
redisClient.expireat(key, timestamp, callback); redisClient.expireat(key, timestamp, callback);
} };
//hashes //hashes
@ -219,15 +212,15 @@
callback(err, res); callback(err, res);
} }
}); });
} };
module.setObjectField = function(key, field, value, callback) { module.setObjectField = function(key, field, value, callback) {
redisClient.hset(key, field, value, callback); redisClient.hset(key, field, value, callback);
} };
module.getObject = function(key, callback) { module.getObject = function(key, callback) {
redisClient.hgetall(key, callback); redisClient.hgetall(key, callback);
} };
module.getObjects = function(keys, callback) { module.getObjects = function(keys, callback) {
var multi = redisClient.multi(); var multi = redisClient.multi();
@ -239,7 +232,7 @@
multi.exec(function (err, replies) { multi.exec(function (err, replies) {
callback(err, replies); callback(err, replies);
}); });
} };
module.getObjectField = function(key, field, callback) { module.getObjectField = function(key, field, callback) {
module.getObjectFields(key, [field], function(err, data) { module.getObjectFields(key, [field], function(err, data) {
@ -249,7 +242,7 @@
callback(null, data[field]); callback(null, data[field]);
}); });
} };
module.getObjectFields = function(key, fields, callback) { module.getObjectFields = function(key, fields, callback) {
redisClient.hmget(key, fields, function(err, data) { redisClient.hmget(key, fields, function(err, data) {
@ -265,48 +258,48 @@
callback(null, returnData); callback(null, returnData);
}); });
} };
module.getObjectKeys = function(key, callback) { module.getObjectKeys = function(key, callback) {
redisClient.hkeys(key, callback); redisClient.hkeys(key, callback);
} };
module.getObjectValues = function(key, callback) { module.getObjectValues = function(key, callback) {
redisClient.hvals(key, callback); redisClient.hvals(key, callback);
} };
module.isObjectField = function(key, field, callback) { module.isObjectField = function(key, field, callback) {
redisClient.hexists(key, field, function(err, exists) { redisClient.hexists(key, field, function(err, exists) {
callback(err, exists === 1); callback(err, exists === 1);
}); });
} };
module.deleteObjectField = function(key, field, callback) { module.deleteObjectField = function(key, field, callback) {
redisClient.hdel(key, field, callback); redisClient.hdel(key, field, callback);
} };
module.incrObjectField = function(key, field, callback) { module.incrObjectField = function(key, field, callback) {
redisClient.hincrby(key, field, 1, callback); redisClient.hincrby(key, field, 1, callback);
} };
module.decrObjectField = function(key, field, callback) { module.decrObjectField = function(key, field, callback) {
redisClient.hincrby(key, field, -1, callback); redisClient.hincrby(key, field, -1, callback);
} };
module.incrObjectFieldBy = function(key, field, value, callback) { module.incrObjectFieldBy = function(key, field, value, callback) {
redisClient.hincrby(key, field, value, callback); redisClient.hincrby(key, field, value, callback);
} };
// sets // sets
module.setAdd = function(key, value, callback) { module.setAdd = function(key, value, callback) {
redisClient.sadd(key, value, callback); redisClient.sadd(key, value, callback);
} };
module.setRemove = function(key, value, callback) { module.setRemove = function(key, value, callback) {
redisClient.srem(key, value, callback); redisClient.srem(key, value, callback);
} };
module.isSetMember = function(key, value, callback) { module.isSetMember = function(key, value, callback) {
redisClient.sismember(key, value, function(err, result) { redisClient.sismember(key, value, function(err, result) {
@ -316,7 +309,7 @@
callback(null, result === 1); callback(null, result === 1);
}); });
} };
module.isMemberOfSets = function(sets, value, callback) { module.isMemberOfSets = function(sets, value, callback) {
var batch = redisClient.multi(); var batch = redisClient.multi();
@ -326,67 +319,67 @@
} }
batch.exec(callback); batch.exec(callback);
} };
module.getSetMembers = function(key, callback) { module.getSetMembers = function(key, callback) {
redisClient.smembers(key, callback); redisClient.smembers(key, callback);
} };
module.setCount = function(key, callback) { module.setCount = function(key, callback) {
redisClient.scard(key, callback); redisClient.scard(key, callback);
} };
module.setRemoveRandom = function(key, callback) { module.setRemoveRandom = function(key, callback) {
redisClient.spop(key, callback); redisClient.spop(key, callback);
} };
// sorted sets // sorted sets
module.sortedSetAdd = function(key, score, value, callback) { module.sortedSetAdd = function(key, score, value, callback) {
redisClient.zadd(key, score, value, callback); redisClient.zadd(key, score, value, callback);
} };
module.sortedSetRemove = function(key, value, callback) { module.sortedSetRemove = function(key, value, callback) {
redisClient.zrem(key, value, callback); redisClient.zrem(key, value, callback);
} };
module.getSortedSetRange = function(key, start, stop, callback) { module.getSortedSetRange = function(key, start, stop, callback) {
redisClient.zrange(key, start, stop, callback); redisClient.zrange(key, start, stop, callback);
} };
module.getSortedSetRevRange = function(key, start, stop, callback) { module.getSortedSetRevRange = function(key, start, stop, callback) {
redisClient.zrevrange(key, start, stop, callback); redisClient.zrevrange(key, start, stop, callback);
} };
module.getSortedSetRevRangeByScore = function(args, callback) { module.getSortedSetRevRangeByScore = function(args, callback) {
redisClient.zrevrangebyscore(args, callback); redisClient.zrevrangebyscore(args, callback);
} };
module.sortedSetCount = function(key, min, max, callback) { module.sortedSetCount = function(key, min, max, callback) {
redisClient.zcount(key, min, max, callback); redisClient.zcount(key, min, max, callback);
} };
module.sortedSetCard = function(key, callback) { module.sortedSetCard = function(key, callback) {
redisClient.zcard(key, callback); redisClient.zcard(key, callback);
} };
module.sortedSetRank = function(key, value, callback) { module.sortedSetRank = function(key, value, callback) {
redisClient.zrank(key, value, callback); redisClient.zrank(key, value, callback);
} };
module.sortedSetRevRank = function(key, value, callback) { module.sortedSetRevRank = function(key, value, callback) {
redisClient.zrevrank(key, value, callback); redisClient.zrevrank(key, value, callback);
} };
module.sortedSetScore = function(key, value, callback) { module.sortedSetScore = function(key, value, callback) {
redisClient.zscore(key, value, callback); redisClient.zscore(key, value, callback);
} };
module.isSortedSetMember = function(key, value, callback) { module.isSortedSetMember = function(key, value, callback) {
module.sortedSetScore(key, value, function(err, score) { module.sortedSetScore(key, value, function(err, score) {
callback(err, !!score); callback(err, !!score);
}); });
} };
module.sortedSetsScore = function(keys, value, callback) { module.sortedSetsScore = function(keys, value, callback) {
var multi = redisClient.multi(); var multi = redisClient.multi();
@ -396,31 +389,28 @@
} }
multi.exec(callback); multi.exec(callback);
} };
// lists // lists
module.listPrepend = function(key, value, callback) { module.listPrepend = function(key, value, callback) {
redisClient.lpush(key, value, callback); redisClient.lpush(key, value, callback);
} };
module.listAppend = function(key, value, callback) { module.listAppend = function(key, value, callback) {
redisClient.rpush(key, value, callback); redisClient.rpush(key, value, callback);
} };
module.listRemoveLast = function(key, callback) { module.listRemoveLast = function(key, callback) {
redisClient.rpop(key, callback); redisClient.rpop(key, callback);
} };
module.listRemoveAll = function(key, value, callback) { module.listRemoveAll = function(key, value, callback) {
redisClient.lrem(key, 0, value, callback); redisClient.lrem(key, 0, value, callback);
} };
module.getListRange = function(key, start, stop, callback) { module.getListRange = function(key, start, stop, callback) {
redisClient.lrange(key, start, stop, callback); redisClient.lrange(key, start, stop, callback);
} };
}(exports)); }(exports));

Loading…
Cancel
Save