cnpm install egg-redis --save
config/plugin.js
enable: true
啓用redisredis
exports.redis = { enable: true, package: 'egg-redis', };
config/config.default.js
config.redis = { client: { port: 6379, // Redis port host: '127.0.0.1', // Redis host password: '', db: 0 } }
app/service/cache.js
if(this.app.redis)
判斷是否有啓用redis數據庫
'use strict'; const Service = require('egg').Service; class CacheService extends Service { async set(key,value,seconds) { value = JSON.stringify(value); if(this.app.redis){ if(!seconds){ await this.app.redis.set(key,value); }else{ await this.app.redis.set(key,value,'EX',seconds) } } } async get(key){ if(this.app.redis){ var data = await this.app.redis.get(key); if(!data) return; return JSON.parse(data) } } } module.exports = CacheService;
app/controller/default/index.js
若是沒有設置redis緩存,就去請求數據,再設置緩存
var topNav = await this.ctx.service.cache.get('index_topNav'); if(!topNav){ topNav=await this.ctx.model.Nav.find({"position":1}); await this.ctx.service.cache.set('index_topNav',topNav,60*60); }
所有代碼npm
'use strict'; const Controller = require('egg').Controller; class IndexController extends Controller { async index() { console.time('index_time') //獲取頂部導航的數據 var topNav = await this.ctx.service.cache.get('index_topNav'); if(!topNav){ topNav=await this.ctx.model.Nav.find({"position":1}); await this.ctx.service.cache.set('index_topNav',topNav,60*60); } //輪播圖 var focus=await this.ctx.service.cache.get('index_focus'); if(!focus){ focus=await this.ctx.model.Focus.find({"type":1}); await this.ctx.service.cache.set('index_focus',focus,60*60); } //商品分類 var goodsCate=await this.ctx.service.cache.get('index_goodsCate'); if(!goodsCate){ goodsCate=await this.ctx.model.GoodsCate.aggregate([ { $lookup:{ from:'goods_cate', localField:'_id', foreignField:'pid', as:'items' } }, { $match:{ "pid":'0' } } ]) await this.ctx.service.cache.set('index_goodsCate',goodsCate,60*60); } // console.log(topNav); var middleNav=await this.ctx.service.cache.get('index_middleNav'); if(!middleNav){ middleNav=await this.ctx.model.Nav.find({"position":2}); middleNav=JSON.parse(JSON.stringify(middleNav)); //一、不可擴展對象 for(var i=0;i<middleNav.length;i++){ if(middleNav[i].relation){ //數據庫查找relation對應的商品 try{ var tempArr=middleNav[i].relation.replace(/,/g,',').split(','); var tempRelationIds=[]; tempArr.forEach((value)=>{ tempRelationIds.push({ "_id":this.app.mongoose.Types.ObjectId(value) }) }) var relationGoods=await this.ctx.model.Goods.find({ $or:tempRelationIds },'title goods_img'); middleNav[i].subGoods=relationGoods; }catch(err){ //二、若是用戶輸入了錯誤的ObjectID(商品id) middleNav[i].subGoods=[]; } }else{ middleNav[i].subGoods=[]; } } await this.ctx.service.cache.set('index_middleNav',middleNav,60*60); } // console.log(JSON.stringify(middleNav)) var shoujiResult=await this.ctx.service.cache.get('index_shoujiResult'); if(!shoujiResult){ shoujiResult=await this.service.goods.get_category_recommend_goods('5bbf058f9079450a903cb77b','best',8); await this.ctx.service.cache.set('index_shoujiResult',shoujiResult,60*60); } console.timeEnd('index_time') await this.ctx.render('default/index',{ topNav:topNav, focus:focus, goodsCate:goodsCate, middleNav:middleNav, shoujiResult:shoujiResult }); } } module.exports = IndexController;