redis application scenarios
redis usage
After installing the configuration environment variables
Open redis instructions: redis-server
Redis connection requires two middleware
$ npm i koa-generic-session koa-redis --save
In the introduction app.js
//引入redis处理中间件
const session = require('koa-generic-session')
const Redis = require('koa-redis')
The session is connected into redis
//app.keys=设置签名密钥(用于cookie和session)
app.keys=['keys','keyskeys']
app.use(session({//将session连接到redis
store:new Redis()//默认执行本机端口
}))
Set session
//设置session
ctx.session.count++
The default results
Of course, you can also customize session settings Property Name
View data stored in redis
Open redis client: redis-cli
View all content stored in the key: keys *
View the content corresponding to the content key: get a specific key value
In operation node directly redis
//引入redis操作
const Redis = require('koa-redis')
//连接redis的客户端服务
const Store = new Redis().client
//直接操作redis
router.get('/fix',async (ctx)=>{
const st = await Store.hset('fix','name',Math.random())//往redis中存值 获取redis中的内容 hget fix name
ctx.body={
code:0
}
})