起因是解决jwt失效处理问题。
1.安装:
mac系统:
brew install redis
安装后,应用程序文件在:/usr/local/bin/目录中(即/usr/local/bin/redis-server),配置文件在:/usr/local/etc/redis.conf
linux系统:
$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz
$ tar xzf redis-4.0.9.tar.gz
$ cd redis-4.0.9
$ make
make完后,redis-4.0.9目录下会有个src目录。里面有redis-server程序。配置文件redis.conf与src目录同级。
-------------------------------
redis-server同级目录下有redis-cli客户端程序用以测试或与redis服务交互,这里先不管。
2.启动服务
配置文件可决定redis如何运行,单纯敲./redis-server启动时,使用的是默认配置文件。
./redis-server path/to/redis.conf //选用配置文件
/usr/local/bin/redis-server ../etc/redis.conf //mac中以默认配置启动
启动后,当前窗口被redis应用占据,另开一个终端,输入redis-cli即可打开redis客户端窗口:
上图命令依次为查看所有健值对、获取键为'test'的值、存入键为'test'的值、获取键为'test'的值、查看所有健值对。
上图命令给数据设置expire缓存时间。
redis还有个flushall命令,用以清空缓存。慎用。
目前redis支持五种数据类型:字符串,哈希,列表,集合、有序集合。
常用命令看菜鸟教程或官网。
3.退出redis
1.进入redis-cli后,交互模式下敲shutdown,回车。
或直接在系统命令行终端敲redis-cli shutdown,回车。
4.nodejs中使用redis
npm安装redis官方推荐的库:
npm install redis
node连接redis:
//Demo
var redis = require('redis');
var client = redis.createClient(port, host);
client.on('connect', function() {
console.log('Redis client connected');
});
client.on('error', function (err) {
console.log('Something went wrong ' + err);
});
...
client.set('key1', 'value1', redis.print);
client.get('key1', function (error, result) {
if (error) {
console.log(error);
throw error;
}
console.log('GET result ->' + result);
});
这个npm插件使用的是 传统的callback形式,其文档给出了promise方案:
//引入bluebird
npm i bluebird
//安装完后,对redis模块进行改造
var bluebird = require('bluebird');
var redis = require('redis');
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
//代码中:
// So instead of writing client.get('foo', cb); you have to write:
return client.getAsync('foo').then(function(res) {
console.log(res); // => 'bar'
});
还有很多生产中的配置和实践的东西,以后慢慢研究。
这里顺便转发一篇 Linux(CentOS)下安装Redis(redis-4.0.1)
参考:
npm-redis
mac下安装配置redis