如果需要配置密码等信息请参考: 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服务器,如下图所示
![7450593-f8d0dd5a1faf8cdb.png](https://upload-images.jianshu.io/upload_images/7450593-f8d0dd5a1faf8cdb.png)
3.redis 命令行工具,双击打开,输入help查看指令
![7450593-56c8a4ce34818134.png](https://upload-images.jianshu.io/upload_images/7450593-56c8a4ce34818134.png)
![7450593-6750fa02285b12e1.png](https://upload-images.jianshu.io/upload_images/7450593-6750fa02285b12e1.png)
4.在项目中安装redis,https://github.com/NodeRedis/node_redis git库
![7450593-76e2503ed91f9fde.png](https://upload-images.jianshu.io/upload_images/7450593-76e2503ed91f9fde.png)
5.导入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);
![7450593-1c158c923040a9b1.png](https://upload-images.jianshu.io/upload_images/7450593-1c158c923040a9b1.png)
8.取值,通过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);
});
![7450593-d2122cf034967a0c.png](https://upload-images.jianshu.io/upload_images/7450593-d2122cf034967a0c.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服务器中存储的值自动清除
![7450593-196405297d323162.png](https://upload-images.jianshu.io/upload_images/7450593-196405297d323162.png)