-
-
Save cliftonc/10532941 to your computer and use it in GitHub Desktop.
Simple LIFO and FIFO queue in Redis
This file contains hidden or 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
var redis = require("redis"), | |
client = redis.createClient(), | |
exitProcessor = false, | |
queueNext = function() { | |
process.nextTick(function() { | |
// Messages are pushed with RPUSH, making this a FIFO queue | |
client.blpop('queue', 1, queueFn); | |
}) | |
}, | |
queueFn = function(err, msg) { | |
if (!err && msg && msg.length === 2) { // Message is always array with two values | |
console.log(msg); | |
} | |
setTimeout(queueNext,10); // pause for 200ms | |
}; | |
queueNext(); | |
// Add messages | |
for(var i=1; i<100; i++) { | |
client.rpush("queue", "Message " + i); | |
} |
This file contains hidden or 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
var redis = require("redis"), | |
client = redis.createClient(), | |
exitProcessor = false, | |
queueNext = function() { | |
process.nextTick(function() { | |
// Messages are pushed with LPUSH, making this a LIFO queue | |
client.blpop('queue', 1, queueFn); | |
}) | |
}, | |
queueFn = function(err, msg) { | |
if (!err && msg && msg.length === 2) { // Message is always array with two values | |
console.log(msg); | |
} | |
setTimeout(queueNext,10); // pause for 200ms | |
}; | |
queueNext(); | |
// Add messages | |
for(var i=1; i<100; i++) { | |
client.lpush("queue", "Message " + i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment