浏览代码

报表数据,中间计量相关,缓存数据增加版本控制

MaiXinRong 5 年之前
父节点
当前提交
40d96c2a86
共有 2 个文件被更改,包括 12 次插入10 次删除
  1. 1 0
      app/lib/stage_im.js
  2. 11 10
      app/service/report_memory.js

+ 1 - 0
app/lib/stage_im.js

@@ -11,6 +11,7 @@
 const Ledger = require('./ledger');
 const imType = require('../const/tender').imType;
 const mergeChar = ';';
+const version = '1.0';
 
 class StageIm {
     constructor (ctx) {

+ 11 - 10
app/service/report_memory.js

@@ -17,6 +17,7 @@ const audit = require('../const/audit');
 const stageImTz = 'mem_stage_im_tz';
 const stageImTzBills = 'mem_stage_im_tz_bills';
 const stageImZl = 'mem_stage_im_zl';
+const stageImVersion = '1.0';
 
 module.exports = app => {
     class ReportMemory extends app.BaseService {
@@ -39,9 +40,9 @@ module.exports = app => {
         // 1. 达到redis内存阈值时,数据会swap到磁盘,此时将消耗IO时间
         // 2. redis单独服务器
         // 3. redis集群
-        async _getReportMemoryCache(name, tid, sid, time) {
+        async _getReportMemoryCache(name, tid, sid, time, version = '') {
             // redis
-            const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
+            const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version;
             const data = await this.cache.get(cacheKey);
             if (data) {
                 return eval(data);
@@ -64,9 +65,9 @@ module.exports = app => {
             // }
         }
 
-        async _setReportMemoryCache(name, tid, sid, time, data) {
+        async _setReportMemoryCache(name, tid, sid, time, data, version = '') {
             // redis
-            const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
+            const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version;
             this.cache.set(cacheKey, JSON.stringify(data), 'EX', this.ctx.app.config.cacheTime);
 
             // mysql + IO
@@ -93,10 +94,10 @@ module.exports = app => {
             this.stageImData.main = stageIm.ImData;
             if (isTz) {
                 this.stageImData.bills = stageIm.ImBillsData;
-                await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
-                await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills);
+                await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion);
+                await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills, stageImVersion);
             } else {
-                await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
+                await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion);
             }
         }
 
@@ -127,7 +128,7 @@ module.exports = app => {
         async getStageImTzData(tid, sid) {
             await this.ctx.service.tender.checkTender(tid);
             await this.ctx.service.stage.checkStage(sid);
-            const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime);
+            const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
             if (cache) {
                 // console.log('cache');
                 return cache;
@@ -152,7 +153,7 @@ module.exports = app => {
         async getStageImTzBillsData(tid, sid) {
             await this.ctx.service.tender.checkTender(tid);
             await this.ctx.service.stage.checkStage(sid);
-            const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime);
+            const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
             if (cache) return cache;
 
             if (!this.stageImData) {
@@ -173,7 +174,7 @@ module.exports = app => {
         async getStageImZlData(tid, sid) {
             await this.ctx.service.tender.checkTender(tid);
             await this.ctx.service.stage.checkStage(sid);
-            const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime);
+            const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
             if (cache) return cache;
 
             this.stageImData = {};