feat: add analytics page data caching

This commit is contained in:
eoao
2026-05-10 12:56:02 +08:00
parent 84e7e2e9d8
commit 09516c9cc8
3 changed files with 41 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ const KvConst = {
AUTH_INFO: 'auth-uid:',
SETTING: 'setting:',
SEND_DAY_COUNT: 'send_day_count:',
ANALYSIS_ECHARTS: 'analysis_echarts:',
PUBLIC_KEY: "public_key:"
}

View File

@@ -5,6 +5,7 @@ import verifyRecordService from './service/verify-record-service';
import emailService from './service/email-service';
import kvObjService from './service/kv-obj-service';
import oauthService from "./service/oauth-service";
import analysisService from './service/analysis-service';
export default {
async fetch(req, env, ctx) {
@@ -24,9 +25,15 @@ export default {
},
email: email,
async scheduled(c, env, ctx) {
if (c.cron === '*/30 * * * *') {
await analysisService.refreshEchartsCache({ env })
return;
}
await verifyRecordService.clearRecord({ env })
await userService.resetDaySendCount({ env })
await emailService.completeReceiveAll({ env })
await oauthService.clearNoBindOathUser({ env })
await analysisService.refreshEchartsCache({ env })
},
};

View File

@@ -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, ''))
};
}
}