refactor: 重构cache的方法

pull/13/head
moonrailgun
parent 38e85c0403
commit 7372c18cfb

@ -2,25 +2,31 @@ import { ChatConverseInfo, fetchConverseInfo } from '../model/converse';
import { fetchUserInfo, UserBaseInfo } from '../model/user'; import { fetchUserInfo, UserBaseInfo } from '../model/user';
import { queryClient } from './index'; import { queryClient } from './index';
function buildCacheFactory<T>( /**
scope: string, *
fetcher: (id: string) => Promise<T> */
) { export async function getCachedUserInfo(
return async (id: string): Promise<T> => { userId: string,
const data = await queryClient.fetchQuery([scope, id], () => fetcher(id)); refetch = false
): Promise<UserBaseInfo> {
const data = await queryClient.fetchQuery(
['user', userId],
() => fetchUserInfo(userId),
{
staleTime: refetch ? 0 : 2 * 60 * 60 * 1000, // 缓存2小时
}
);
return data; return data;
};
} }
/** /**
* *
*/ */
export const getCachedUserInfo = buildCacheFactory<UserBaseInfo>( export async function getCachedConverseInfo(
'user', converseId: string
fetchUserInfo ): Promise<ChatConverseInfo> {
); const data = await queryClient.fetchQuery(['converse', converseId], () =>
fetchConverseInfo(converseId)
export const getCachedConverseInfo = buildCacheFactory<ChatConverseInfo>( );
'converse', return data;
fetchConverseInfo }
);

Loading…
Cancel
Save