feat: add analytics page data caching
This commit is contained in:
@@ -9,7 +9,30 @@ import { toUtc } from '../utils/date-uitil';
|
||||
const analysisService = {
|
||||
|
||||
async echarts(c, params) {
|
||||
const cacheKey = this.echartsCacheKey(params);
|
||||
const cache = await c.env.kv.get(cacheKey, { type: 'json' });
|
||||
|
||||
if (cache) {
|
||||
return cache;
|
||||
}
|
||||
|
||||
return await this.refreshEchartsCacheByKey(c, cacheKey);
|
||||
},
|
||||
|
||||
async refreshEchartsCacheByKey(c, cacheKey) {
|
||||
const params = this.echartsParamsByCacheKey(cacheKey);
|
||||
const data = await this.queryEcharts(c, params);
|
||||
await c.env.kv.put(cacheKey, JSON.stringify(data));
|
||||
return data;
|
||||
},
|
||||
|
||||
async refreshEchartsCache(c) {
|
||||
const { keys } = await c.env.kv.list({ prefix: kvConst.ANALYSIS_ECHARTS });
|
||||
|
||||
await Promise.all(keys.map(key => this.refreshEchartsCacheByKey(c, key.name)));
|
||||
},
|
||||
|
||||
async queryEcharts(c, params) {
|
||||
|
||||
const { timeZone } = params;
|
||||
|
||||
@@ -84,6 +107,16 @@ const analysisService = {
|
||||
return {date: day,total}
|
||||
})
|
||||
|
||||
},
|
||||
|
||||
echartsCacheKey(params = {}) {
|
||||
return kvConst.ANALYSIS_ECHARTS + encodeURIComponent(params.timeZone || 'UTC');
|
||||
},
|
||||
|
||||
echartsParamsByCacheKey(cacheKey) {
|
||||
return {
|
||||
timeZone: decodeURIComponent(cacheKey.replace(kvConst.ANALYSIS_ECHARTS, ''))
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user