diff --git a/services/groups.ts b/services/groups.ts index 64c3c0d..5995385 100644 --- a/services/groups.ts +++ b/services/groups.ts @@ -6,19 +6,24 @@ export interface GroupMeta { createdAt: Date; } +const groupMetaCache = new Map(); + export const groupService = { - async getMetaOfGroup (id: number): Promise> { - const res = await $fetch('/api/groups', { - method: 'get', - query: { - id, - }, - }); + async getMetaOfGroup(id: number): Promise> { + if (!groupMetaCache.has(id)) { + const res = await $fetch('/api/groups', { + method: 'get', + query: { + id, + }, + }); + groupMetaCache.set(id, res); + } + const res = groupMetaCache.get(id) as any; if (res.error) { return new Err({ code: res.error.code, message: res.error.message }); } const { ok: { data } } = res; return new Ok({ name: data.name, id: data.id, createdAt: data.createdAt }); - }, }; diff --git a/services/users.ts b/services/users.ts index e8de9f8..212d2e5 100644 --- a/services/users.ts +++ b/services/users.ts @@ -7,15 +7,21 @@ export interface UserMeta { createdAt: Date; } +const userMetaCache = new Map(); + export const userService = { async getMetaOfUser(id: number): Promise> { - const res = await $fetch('/api/users', { - method: 'get', - query: { - id, - }, - credentials: 'include', - }); + if (!userMetaCache.has(id)) { + const res = await $fetch('/api/users', { + method: 'get', + query: { + id, + }, + credentials: 'include', + }); + userMetaCache.set(id, res); + } + const res = userMetaCache.get(id) as any; if (res.error) { return new Err({ code: res.error.code, message: res.error.message }); }