koa2 redis

const Redis = require('ioredis')
const redis = {
    port: 6379,          // Redis port
    host: '**',   // Redis host
    prefix: 'pon:', //存诸前缀
    ttl: 60 * 60 * 23,  //过期时间   
    family: 4,
    db: 0
}
const newRedis = new Redis(redis)

function  get(key){
    return new Promise(function (resolve, reject) {
        newRedis.get(key, function (err, result) {  
            if(err) reject(err)
            console.log(result)
            resolve(result);
          });  
    })
};

//newRedis.set('a',"haha")
module.exports.newRedis = newRedis
module.exports.RedisGet = get

引用

const redis = require("../../common/redis")
const newRedis = redis.newRedis
const RedisGet = redis.RedisGet

router.get('/list', async (ctx, next) => {
    let rows = await RedisGet('/list')
    if(rows != null){
         console.log('rows = ',rows)
         ctx.response.body =rows;
         return;
    }
    let OuVersions = await OuVersion.findAll({});
    ctx.response.body ={Ou:OuVersions,} ;
    newRedis.set('/list',JSON.stringify({Ou:OuVersions,}))
});

 

上一篇:20-移动web开发-rem布局


下一篇:2.koa2中用mongoose连接Mongodb