Skip to content

Instantly share code, notes, and snippets.

@JacobHsu
Last active January 16, 2023 15:54
Show Gist options
  • Save JacobHsu/346a869919c5d67a44e7 to your computer and use it in GitHub Desktop.
Save JacobHsu/346a869919c5d67a44e7 to your computer and use it in GitHub Desktop.
#nodejs redis list rpush lpush brpop blpop
var redis = require('redis'),
client = redis.createClient();
var arr = ["some val","some val2","some val3"];
//Use multi() to pipeline multiple commands at once
var multi = client.multi();
for (var i=0; i<arr.length; i++) {
//console.log(arr[i]);
//將一個或多個值value插入到列表key的表尾。
multi.rpush('testlist', arr[i]);
}
multi.exec(function(err, response) {
if(err) throw err;
})
/* SHOW ALL LIST ITEMS--*/
client.lrange('testlist', 0, -1, function (error, items) {
if (error) throw error
items.forEach(function (item) {
/// processItem(item)
console.log(item);
})
console.log('===');
})
// LPOP移除并返回列表key的头元素。
// blpop LPOP命令的阻塞版本
client.blpop('testlist', 0, function(err, data) {
console.log(' blpop: ' + data[1]);
//setTimeout(function() {
// addWorker();
//}, 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment