refactor: 重构cache的方法

pull/13/head
moonrailgun 4 years ago
parent 38e85c0403
commit 7372c18cfb

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

Loading…
Cancel
Save