javascript 节点--redis.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 节点--redis.js相关的知识,希望对你有一定的参考价值。

##redis有設密碼的初始化方式
redisClient = redis.createClient({host:config.redisIP, port:config.redisPort, password:config.redisPwd});



## connect redis
var redis = require('redis'),
    client = redis.createClient(6379, '127.0.0.1',{});


## set key to redis
client.on('connect', function(){
    client.set('author:001', 'jacky');
    client.set('author:002', 'fufu');
    console.log('ok...insert');
});


## get key to redis
client.on('connect', function(){
	client.get('author:001', function(err, data){
        console.log(data);
    });
});



#####################################
##nodejs + redis訂閱/發佈

//1- 訂閱端
var redis = require('redis');
// createClient里面可以带代option
var subscriber = redis.createClient(6379, '127.0.0.1',{});
// subscribe channgel
subscriber.subscribe('channel_1');
subscriber.subscribe('channel_2');
// receive message
subscriber.on('message', function(channel, message){

    if (channel === 'channel_1') {
        console.log('頻道1: 收到消息了---');
    }else if(channel === 'channel_2'){
        console.log('頻道2: 收到消息了---');
    }
});

//2- 發佈端
var redis = require('redis');
var publisher = redis.createClient(6379, '127.0.0.1',{});
publisher.publish('channel_1', '發送消息頻道-1');
// publisher.publish('channel_2', '發送消息頻道-2');


#===================================================================
var redis = require('redis');
var redisClient = redis.createClient(6379, '127.0.0.1', {});

##Queue生產者
let msg = '寫入隊列測試';
redisClient.lpush('MSGQ', JSON.stringify(msg), (err, data) => {
	if (err) {
		console.log(err);
		return;
	}
	console.log(data);
	redisClient.quit();
});


##Queue消費者
var redis = require('redis');
var redisClient = redis.createClient(6379, '127.0.0.1', {});

let customQueue = () => {

	setInterval(() => {
		redisClient.brpop('MSGQ', 0, (err, data) => {
			console.log(data);
		});
	}, 2000);

}

customQueue();


以上是关于javascript 节点--redis.js的主要内容,如果未能解决你的问题,请参考以下文章

javascript节点

javaScript(叁)

javascript 怎样获取节点文本值?

javascript如何获取子孙节点?

JavaScript Dom节点

Javascript进阶篇——(DOM—节点---属性访问节点)—笔记整理