Skip to content

Instantly share code, notes, and snippets.

@dannycoates
Created March 14, 2015 02:18
Show Gist options
  • Save dannycoates/556cac64c88e14f9f0bb to your computer and use it in GitHub Desktop.
Save dannycoates/556cac64c88e14f9f0bb to your computer and use it in GitHub Desktop.
notification log
var redis = require('redis')
function match(event, filter) {
var filterNames = Object.keys(filter)
for (var i = 0; i < filterNames.length; i++) {
var name = filterNames[i]
if (event[name] && event[name] === filter[name]) {
continue
}
else {
return false
}
}
return true
}
function RedisDB(options) {
this.db = redis.createClient(options.port, options.host)
}
RedisDB.create = function (options, cb) {
var x = new RedisDB(options)
x.db.once('ready', cb.bind(null, null, x))
}
RedisDB.prototype.append = function (str, cb) {
this.db.rpush('fxa', str, cb)
}
RedisDB.prototype.read = function (pos, num, filter, cb) {
pos = +pos || 0
num = num || 1000
filter = filter || {}
this.db.lrange('fxa', pos, (pos + num), function (err, results) {
if (err) { return cb(err) }
var events = results.filter(function (event) {
return match(event, filter)
})
cb(
null,
{
next_pos: (pos + results.length).toString(),
events: events
}
)
})
}
RedisDB.prototype.head = function (cb) {
this.db.llen('fxa', function (err, len) {
cb(err, len && len.toString())
})
}
RedisDB.prototype.tail = function (cb) {
process.nextTick(cb.bind(null, null, '0'))
}
module.exports = RedisDB
@dannycoates
Copy link
Author

@rfk here's a minimal redis implementation. Besides some encoding/decoding nits of the JWTs it works.

@dannycoates
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment