Skip to content

Commit

Permalink
Changed the 'messages' method in Redis from hash to sorted set
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Campos committed Sep 6, 2017
1 parent eda2ff8 commit a37d59e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
10 changes: 8 additions & 2 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ env(__dirname + '/.env');
var Mariam = require('./lib/mariam.js');

// Storage
var redisConfig = { "methods": ['subscriptions', 'topics', 'messages'], "url": process.env.REDIS_URL };
var storage = require('botkit-storage-redis')(redisConfig);
var redisConfig = {
"hash_methods": ['subscriptions', 'topics'],
"sorted_set_methods": ['messages'],
"url": process.env.REDIS_URL,
"namespace": "mariam"
};

var storage = require('./lib/storage.js')(redisConfig);

//
// BotKit initialization
Expand Down
2 changes: 1 addition & 1 deletion lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function Messages(configuration) {
webserver.get('/messages', function(req, res) {

var messages = [];
controller.config.storage.messages.all(function(err, message_data) {
controller.config.storage.messages.zget(req.query.from, req.query.to, function(err, message_data) {
if (message_data) {
message_data.forEach(function(message) {
messages.push(message);
Expand Down
3 changes: 1 addition & 2 deletions lib/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ function Notifications(configuration) {

var date = new Date();
var time = date.getTime();
message_uid = topic + ":" + time;

controller.config.storage.messages.save({ id: message_uid, topic: topic, time: time, message: message }, function(err) {});
controller.config.storage.messages.zsave({ topic: topic, time: time, message: message }, time, function(err) {});
}

return controller;
Expand Down
45 changes: 45 additions & 0 deletions lib/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var redis = require('redis');

module.exports = function(config) {
var storage = require('botkit-storage-redis')({
"methods": config.hash_methods,
"url": config.url,
"namespace": config.namespace
});

var client = redis.createClient(config);
var methods = config.sorted_set_methods;
var extended_storage = {};

for (var i = 0; i < methods.length; i++) {
extended_storage[methods[i]] = getStorageObj(client, config.namespace + ':' + methods[i]);
}

Object.assign(storage, extended_storage);

return storage;
};

function getStorageObj(client, namespace) {
return {
zget: function(min, max, cb, options) {
client.zrangebyscore(namespace, min, max, function(err, res) {
if (err) {
return cb(err);
}
var parsed,
array = [];

for (var i in res) {
parsed = JSON.parse(res[i]);
res[i] = parsed;
array.push(parsed);
}
cb(null, options && options.type === 'object' ? res : array);
});
},
zsave: function(object, score, cb) {
client.zadd(namespace, score, JSON.stringify(object), redis.print);
}
};
}

0 comments on commit a37d59e

Please sign in to comment.