From 5816ebb5e6a4cc54bd9ba25ff023a2cc3ce2f20d Mon Sep 17 00:00:00 2001 From: iven Date: Fri, 22 May 2026 12:11:59 +0800 Subject: [PATCH] =?UTF-8?q?perf(mp):=20=E7=BC=93=E5=AD=98=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20=E2=80=94=20restore=20=E6=9D=A1=E4=BB=B6=E6=B8=85?= =?UTF-8?q?=E7=90=86=20+=20LRU=20+=20=E5=B7=AE=E5=BC=82=E5=8C=96=20TTL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - auth store: clearRequestCache 仅在 user/roles/patient 变更时调用 - ResponseCache: get() 命中时 delete+set 更新 Map 顺序(真 LRU) - 告警 API 缓存 TTL 10s,通知未读数 TTL 10s,其余保持 60s 默认 --- apps/miniprogram/src/services/alert.ts | 2 +- apps/miniprogram/src/services/notification.ts | 2 +- apps/miniprogram/src/services/request/cache.ts | 14 +++++++++++--- apps/miniprogram/src/stores/auth.ts | 5 ++--- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/apps/miniprogram/src/services/alert.ts b/apps/miniprogram/src/services/alert.ts index 0b30bf2..6226b1d 100644 --- a/apps/miniprogram/src/services/alert.ts +++ b/apps/miniprogram/src/services/alert.ts @@ -22,5 +22,5 @@ export async function listPatientAlerts(patientId: string, params?: { status?: s page: params?.page ?? 1, page_size: params?.page_size ?? 20, status: params?.status, - }); + }, 10_000); } diff --git a/apps/miniprogram/src/services/notification.ts b/apps/miniprogram/src/services/notification.ts index 9a2bf72..2e63754 100644 --- a/apps/miniprogram/src/services/notification.ts +++ b/apps/miniprogram/src/services/notification.ts @@ -8,5 +8,5 @@ export const notificationService = { markAllRead: () => api.put('/messages/read-all'), getUnreadCount: () => - api.get('/messages/unread-count'), + api.get('/messages/unread-count', undefined, 10_000), }; diff --git a/apps/miniprogram/src/services/request/cache.ts b/apps/miniprogram/src/services/request/cache.ts index c97aa39..85aabe9 100644 --- a/apps/miniprogram/src/services/request/cache.ts +++ b/apps/miniprogram/src/services/request/cache.ts @@ -28,9 +28,17 @@ export class ResponseCache { } get(url: string): T | null { - const entry = this.cache.get(this.cacheKey(url)); - if (entry && Date.now() < entry.expiry) return entry.data as T; - return null; + const key = this.cacheKey(url); + const entry = this.cache.get(key); + if (!entry) return null; + if (Date.now() >= entry.expiry) { + this.cache.delete(key); + return null; + } + // LRU: 命中时更新插入顺序,使该条目移到末尾 + this.cache.delete(key); + this.cache.set(key, entry); + return entry.data as T; } getInflight(url: string): Promise | null { diff --git a/apps/miniprogram/src/stores/auth.ts b/apps/miniprogram/src/stores/auth.ts index 6127d52..5753c10 100644 --- a/apps/miniprogram/src/stores/auth.ts +++ b/apps/miniprogram/src/stores/auth.ts @@ -109,9 +109,6 @@ export const useAuthStore = create((set, get) => ({ setCachedPatientId(currentPatient.id); } - // 状态有变化时清理请求缓存,避免返回过期数据 - clearRequestCache(); - // 跳过未变更的 set() const cur = get(); const userChanged = cur.user?.id !== user?.id; @@ -119,6 +116,8 @@ export const useAuthStore = create((set, get) => ({ const patientChanged = cur.currentPatient?.id !== currentPatient?.id; if (!userChanged && !rolesChanged && !patientChanged) return; + // 状态有变化时清理请求缓存,避免返回过期数据 + clearRequestCache(); set({ user, roles, currentPatient }); },