node.js应用Redis

版权声明:如需转载本文章,请保留出处! https://blog.csdn.net/xc_zhou/article/details/86615484

Node.js下使用Redis,首先:

1、有一台安装了Redis的服务器,当然,安装在本机也行

2、本机,也就是客户端,要装node.js

3、项目要安装nodejs_redis模块

注意第 3 点,不是在本机安装就行了,而是说,要在项目中安装(引用)。

方法是,DOS窗口,在项目目录下,输入

npm install redis

这样就将nodejs_redis下载一份,放到当前目录下了。看看,多了一个文件夹:node_modules\redis

编写以下代码,保存到当前目录下\index.js

var redis = require("redis"),//召唤redis
/*
    连接redis数据库,createClient(port,host,options);
    如果REDIS在本机,端口又是默认,直接写createClient()即可
    redis.createClient() = redis.createClient(6379, '127.0.0.1', {})
*/
    RDS_PORT = 6379,                //端口号
    RDS_HOST = '192.168.8.30',    //服务器IP  要连接的A服务器redis
    RDS_PWD = '123456',     //密码
    RDS_OPTS = {db:9},                  //设置项
    client = redis.createClient(RDS_PORT,RDS_HOST,RDS_OPTS);

//如果需要验证,还要进行验证
//client.auth(password, callback);
client.auth(RDS_PWD,function(){
    console.log('通过认证');
});
// if you'd like to select database 3, instead of 0 (default), call
// client.select(3, function() { /* ... */ });

client.on('connect',function(){
    //redis.print,回调函数,将redis的返回值显示出来。上一句执行结果,将返回“OK”
    client.set('author', 'Wilson',redis.print);
    // 设置过期时间 10s后过期
    //client.set('key','value','EX',10)
    client.get('author', redis.print);
    console.log('connect');
});
client.on('ready',function(err){
    console.log('ready');
});

//错误监听?
client.on("error", function (err) {
    console.log("Error " + err);
});

client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
//遍历哈希表"hash key"
client.hkeys("hash key", function (err, replies) {
    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
client.hget("hash key","hashtest 1",redis.print);

//可同时设置多个key,value
client.hmset('hash 1', 'key', 'value111', 'key2', 'value222', 'key3', 'value3', redis.print);
//可同时获取多个key
client.hmget('hash 1', 'key', 'key2', 'key3', redis.print);

/*两种都可以断掉与redis的连接,
end()很粗暴,不管3721,一下子退出来了,上面那句获取哈希表"hash key"的某个元素值的表达式将没有结果返回
而quit()则是先将语句处理完毕再干净地退出,斯文得很
*/
//client.end();
client.quit();
});

运行:

DOS窗口,当前项目目录下,输入

node index.js

  • publish/subscribe(发布/订阅)
const sub = redis.createClient() // 订阅者
const pub = redis.createClient() // 发布者
var msg_count = 0;

sub.on("subscribe", function (channel, count) {
    client.publish("a nice channel", "I am sending a message.");
    client.publish("a nice channel", "I am sending a second message.");
    client.publish("a nice channel", "I am sending my last message.");
});

sub.on("message", function (channel, message) {
    console.log("sub channel " + channel + ": " + message);
    msg_count += 1;
    if (msg_count === 3) {
        sub.unsubscribe();
        sub.quit();
        client.quit();
    }
});

ps aux|grep redis //查看开启的redis
kill -9 1245(redis的开启号) //杀死开启程序

参考资料:https://github.com/mranney/node_redis

参考:https://blog.csdn.net/q1512451239/article/details/56488686
https://www.e-learn.cn/content/javascript/1079912

猜你喜欢

转载自blog.csdn.net/xc_zhou/article/details/86615484