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:
@@ -22,5 +22,5 @@ export async function listPatientAlerts(patientId: string, params?: { status?: s
|
|||||||
page: params?.page ?? 1,
|
page: params?.page ?? 1,
|
||||||
page_size: params?.page_size ?? 20,
|
page_size: params?.page_size ?? 20,
|
||||||
status: params?.status,
|
status: params?.status,
|
||||||
});
|
}, 10_000);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,5 +8,5 @@ export const notificationService = {
|
|||||||
markAllRead: () =>
|
markAllRead: () =>
|
||||||
api.put('/messages/read-all'),
|
api.put('/messages/read-all'),
|
||||||
getUnreadCount: () =>
|
getUnreadCount: () =>
|
||||||
api.get('/messages/unread-count'),
|
api.get('/messages/unread-count', undefined, 10_000),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -28,9 +28,17 @@ export class ResponseCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get<T>(url: string): T | null {
|
get<T>(url: string): T | null {
|
||||||
const entry = this.cache.get(this.cacheKey(url));
|
const key = this.cacheKey(url);
|
||||||
if (entry && Date.now() < entry.expiry) return entry.data as T;
|
const entry = this.cache.get(key);
|
||||||
return null;
|
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 {
|
getInflight<T>(url: string): Promise<T> | null {
|
||||||
|
|||||||
@@ -109,9 +109,6 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
|||||||
setCachedPatientId(currentPatient.id);
|
setCachedPatientId(currentPatient.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 状态有变化时清理请求缓存,避免返回过期数据
|
|
||||||
clearRequestCache();
|
|
||||||
|
|
||||||
// 跳过未变更的 set()
|
// 跳过未变更的 set()
|
||||||
const cur = get();
|
const cur = get();
|
||||||
const userChanged = cur.user?.id !== user?.id;
|
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;
|
const patientChanged = cur.currentPatient?.id !== currentPatient?.id;
|
||||||
if (!userChanged && !rolesChanged && !patientChanged) return;
|
if (!userChanged && !rolesChanged && !patientChanged) return;
|
||||||
|
|
||||||
|
// 状态有变化时清理请求缓存,避免返回过期数据
|
||||||
|
clearRequestCache();
|
||||||
set({ user, roles, currentPatient });
|
set({ user, roles, currentPatient });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user