From 09516c9cc889e0bbc3b36d43d8b9762d501f4da5 Mon Sep 17 00:00:00 2001 From: eoao Date: Sun, 10 May 2026 12:56:02 +0800 Subject: [PATCH] feat: add analytics page data caching --- mail-worker/src/const/kv-const.js | 1 + mail-worker/src/index.js | 7 +++++ mail-worker/src/service/analysis-service.js | 33 +++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/mail-worker/src/const/kv-const.js b/mail-worker/src/const/kv-const.js index fc60202..1bbe64f 100644 --- a/mail-worker/src/const/kv-const.js +++ b/mail-worker/src/const/kv-const.js @@ -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:" } diff --git a/mail-worker/src/index.js b/mail-worker/src/index.js index 4c950cd..10a24dc 100644 --- a/mail-worker/src/index.js +++ b/mail-worker/src/index.js @@ -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 }) }, }; diff --git a/mail-worker/src/service/analysis-service.js b/mail-worker/src/service/analysis-service.js index d8391df..d46b40d 100644 --- a/mail-worker/src/service/analysis-service.js +++ b/mail-worker/src/service/analysis-service.js @@ -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, '')) + }; } }