node.js中使用Redis


服務端:

啟動Redis服務: redis-server

 

客戶端:

1、安裝Redis  

     npm install redis --save

2、redisTest.js文件

//引入redis
var redis = require("redis");
//創建redis客戶端
var client = redis.createClient("6379", "127.0.0.1");
//連接錯誤處理
client.on("error", function (error) {
console.log(error);
});
//redis驗證 (如果redis沒有開啟驗證,此配置可以不寫)
//client.auth("123");
//查找

client.select("15", function (error) {
if (error) {
console.log(error);
} else {
client.set("mykey","please 888");
client.get("mykey",function(err,res){
console.log("get mykey:",res);
})
client.hmset("mykey2",["name","mary2","age",100],function(error,res){
console.log("res:",res);
})

client.hget("mykey2","name",function(err,res){
console.log("name:", res);
})


client.set("node_redis_key", JSON.stringify({ "name": "wolfy", age: 28 }), function (error, res) {
if (error) {
//console.log(error);
} else {
//console.log(res);
};
//操作完成,關閉redis連接
client.end(true);

});
};
});

3、執行文件:node redisTest.js


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM