如果需要配置密码等信息请参考: https://www.jianshu.com/p/07950a79345b
或者:http://www.runoob.com/redis/redis-conf.html
1.安装redis服务器
https://github.com/MSOpenTech/redis/releases windows 64位地址
2.将zip压缩包解压,cmd进入,输入 redis-server redis.windows.conf 运行redis服务器,如下图所示
3.redis 命令行工具,双击打开,输入help查看指令
image.png image.png4.在项目中安装redis,https://github.com/NodeRedis/node_redis git库
image.png5.导入redis ,创建redis服务器(可本地,可远程,通过设置createClient的options更改)
var redis = require("redis"),
client = redis.createClient();
6.error捕获
client.on("error", function (err) {
console.log("Error " + err);
});
7.设置要保存的值,通过set设置键值,可以是数组的形式,redis.print可有可无,如果有redis.print,则可以打印是否保存成功,如下图
client.set("first", "some val");
client.set("second", "some val",redis.print);//ok
client.set(["first", "some val"]);//ok
client.set(["second", "some vals"],redis.print);
image.png8.取值,通过get方法取出保存在redis服务器中的值
client.get("first", function(err, reply) {
// reply is null when the key is missing
console.log(reply);
});
client.get("second", function(err, reply) {
// reply is null when the key is missing
console.log(reply);
});
image.png
9.设置自动过期时间
// this key will expires after 10 seconds
client.set('key', 'value!', 'EX', 10);
client.set("second", "some val",'EX',10,redis.print);
client.get("second", function(err, reply) {
// reply is null when the key is missing
console.log(reply);
});
第一次运行输出second的值,10s钟后将client.set("second", "some val",'EX',10,redis.print);注释,输出null,redis服务器中存储的值自动清除
image.png
网友评论