perf(mp): 缓存优化 — restore 条件清理 + LRU + 差异化 TTL

- auth store: clearRequestCache 仅在 user/roles/patient 变更时调用
- ResponseCache: get() 命中时 delete+set 更新 Map 顺序(真 LRU)
- 告警 API 缓存 TTL 10s,通知未读数 TTL 10s,其余保持 60s 默认
This commit is contained in:
iven
2026-05-22 12:11:59 +08:00
parent 22e33114b1
commit 5816ebb5e6
4 changed files with 15 additions and 8 deletions

View File

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

View File

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

View File

@@ -28,9 +28,17 @@ export class ResponseCache {
}
get<T>(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<T>(url: string): Promise<T> | null {

View File

@@ -109,9 +109,6 @@ export const useAuthStore = create<AuthState>((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<AuthState>((set, get) => ({
const patientChanged = cur.currentPatient?.id !== currentPatient?.id;
if (!userChanged && !rolesChanged && !patientChanged) return;
// 状态有变化时清理请求缓存,避免返回过期数据
clearRequestCache();
set({ user, roles, currentPatient });
},