From 67cc41c1813e5969d3d6c56f1476da22ee7ec590 Mon Sep 17 00:00:00 2001 From: valign <78541912+cuvalign@users.noreply.github.com> Date: Fri, 5 Jul 2024 18:12:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(mis):=20=E5=B9=B3=E5=8F=B0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=9F=E8=AE=A1=E7=BC=93=E5=AD=98=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20(#1336)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 平台管理->平台数据统计: 这些数据滞后有误且有缓存。干扰了整个页面可视图表何数据的准确性。 以账户充值为例类推 ## 修改前 ### 进入时 可视图表显示本周没有账户充值 本例中为2024-04-17 -> 2024-04-24 ![1713950395375](https://github.com/PKUHPC/scow-internal-dev/assets/78541912/fcd542ca-9492-4924-9c6e-d75e0e07516f) ### 调整日期筛选到今天 2024-04-24 -> 2024-04-24 可见有账户充值 ![1713950512545](https://github.com/PKUHPC/scow-internal-dev/assets/78541912/e38084a9-e733-4e55-80a8-40a7a59b0230) 但是此时筛选回到2024-04-17 -> 2024-04-24 仍然没有账户充值(被缓存) 切换成2024-04-23 -> 2024-04-24 则有账户充值 因此,应当修改本页面,使平台数据统计数据显示正确,避免错误的缓存无法变更。 ## 修改后 > 默认缓存时间为5分钟 ![image](https://github.com/PKUHPC/SCOW/assets/78541912/2495a834-9090-40b5-936e-3f5ee486904e) --- .changeset/great-ties-join.md | 5 ++++ apps/mis-server/src/entities/QueryCache.ts | 2 +- apps/mis-server/src/utils/cache.ts | 27 ++++++++++++++++++---- 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 .changeset/great-ties-join.md diff --git a/.changeset/great-ties-join.md b/.changeset/great-ties-join.md new file mode 100644 index 0000000000..52b1e9410e --- /dev/null +++ b/.changeset/great-ties-join.md @@ -0,0 +1,5 @@ +--- +"@scow/mis-server": patch +--- + +平台数据统计缓存函数设置缓存时间变量,并默认为 5 分钟 diff --git a/apps/mis-server/src/entities/QueryCache.ts b/apps/mis-server/src/entities/QueryCache.ts index 24aab7f44f..17d4800404 100644 --- a/apps/mis-server/src/entities/QueryCache.ts +++ b/apps/mis-server/src/entities/QueryCache.ts @@ -38,6 +38,6 @@ export class QueryCache { } this.queryKey = init.queryKey; this.queryResult = init.queryResult; - this.timestamp = new Date(); + this.timestamp = init.timestamp ?? new Date(); } } diff --git a/apps/mis-server/src/utils/cache.ts b/apps/mis-server/src/utils/cache.ts index 1a94077e33..1e4f861655 100644 --- a/apps/mis-server/src/utils/cache.ts +++ b/apps/mis-server/src/utils/cache.ts @@ -13,21 +13,38 @@ import { MySqlDriver, QueryBuilder, SqlEntityManager } from "@mikro-orm/mysql"; import { QueryCache } from "src/entities/QueryCache"; -export const queryWithCache = async ({ em, queryKeys, queryQb }: { +export const queryWithCache = async ({ em, queryKeys, queryQb, cacheOptions }: { em: SqlEntityManager queryKeys: string[], queryQb: QueryBuilder, + cacheOptions?: { + maxAgeMilliseconds?: number, + enabled?: boolean, + }, }) => { + const { maxAgeMilliseconds = 5 * 60 * 1000, enabled = true } = cacheOptions || {}; + + if (!enabled) { + return await queryQb.execute(); + } const queryKey = queryKeys.join(":"); - const queryCache = await em.findOne(QueryCache, { - queryKey, - }); + const queryCache = await em.findOne(QueryCache, { queryKey }); + + const now = new Date(); if (!queryCache) { const newResult = await queryQb.execute(); - const queryCache = new QueryCache({ queryKey, queryResult: newResult }); + const newQueryCache = new QueryCache({ queryKey, queryResult: newResult, timestamp: now }); + await em.persistAndFlush(newQueryCache); + return newResult; + } + + if ((now.getTime() - queryCache.timestamp.getTime()) > maxAgeMilliseconds) { + const newResult = await queryQb.execute(); + queryCache.queryResult = newResult; + queryCache.timestamp = now; await em.persistAndFlush(queryCache); return newResult; } else {