Skip to content

Instantly share code, notes, and snippets.

@cliftonc
Last active September 22, 2022 06:13
Show Gist options
  • Save cliftonc/10532941 to your computer and use it in GitHub Desktop.
Save cliftonc/10532941 to your computer and use it in GitHub Desktop.
Simple LIFO and FIFO queue in Redis
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);
}
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