forked from AltusConsulting/Spark-Broadcast-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the 'messages' method in Redis from hash to sorted set
- Loading branch information
Rafael Campos
committed
Sep 6, 2017
1 parent
eda2ff8
commit a37d59e
Showing
4 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} |