服务端:
启动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