From 7372c18cfba8ab70beff1848778b3a114c46f2dc Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 30 Jul 2021 15:12:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84cache=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/cache/cache.ts | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/shared/cache/cache.ts b/shared/cache/cache.ts index d01e14fb..f63ff292 100644 --- a/shared/cache/cache.ts +++ b/shared/cache/cache.ts @@ -2,25 +2,31 @@ import { ChatConverseInfo, fetchConverseInfo } from '../model/converse'; import { fetchUserInfo, UserBaseInfo } from '../model/user'; import { queryClient } from './index'; -function buildCacheFactory( - scope: string, - fetcher: (id: string) => Promise -) { - return async (id: string): Promise => { - const data = await queryClient.fetchQuery([scope, id], () => fetcher(id)); - return data; - }; -} - /** * 获取缓存的用户信息 */ -export const getCachedUserInfo = buildCacheFactory( - 'user', - fetchUserInfo -); +export async function getCachedUserInfo( + userId: string, + refetch = false +): Promise { + const data = await queryClient.fetchQuery( + ['user', userId], + () => fetchUserInfo(userId), + { + staleTime: refetch ? 0 : 2 * 60 * 60 * 1000, // 缓存2小时 + } + ); + return data; +} -export const getCachedConverseInfo = buildCacheFactory( - 'converse', - fetchConverseInfo -); +/** + * 获取缓存的会话信息 + */ +export async function getCachedConverseInfo( + converseId: string +): Promise { + const data = await queryClient.fetchQuery(['converse', converseId], () => + fetchConverseInfo(converseId) + ); + return data; +}