Skip to content

Commit 5165035

Browse files
committed
Add caching to gateway
1 parent ed414c4 commit 5165035

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

TechStacks.Client/src/lib/api/gateway.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,40 +109,58 @@ export const logout = async () => {
109109
// TECHNOLOGIES
110110
// ============================================
111111

112+
let allTechnologiesCache: dtos.Technology[] | null = null;
113+
let technologyTiersCache: any[] | null = null;
114+
const technologyCache = new Map<string, any>();
115+
116+
function clearTechnologiesCache() {
117+
allTechnologiesCache = null;
118+
technologyTiersCache = null;
119+
technologyCache.clear();
120+
}
121+
112122
export const getTechnology = async (slug: string) => {
123+
const cached = technologyCache.get(slug);
124+
if (cached) return cached;
113125
const request = new dtos.GetTechnology();
114126
request.slug = slug;
115127
const response = await client.get(request);
116-
return {
128+
const fullTechnology = {
117129
...response.technology,
118130
technologyStacks: response.technologyStacks
119131
};
132+
technologyCache.set(slug, fullTechnology);
133+
return fullTechnology;
120134
};
121135

122136
export const getAllTechnologies = async () => {
123-
return await client.get(new dtos.GetAllTechnologies(), { include: 'total' });
137+
if (!allTechnologiesCache) {
138+
const response = await client.get(new dtos.GetAllTechnologies(), { include: 'total' });
139+
allTechnologiesCache = response.results ?? [];
140+
}
141+
return allTechnologiesCache;
124142
};
125143

126144
export const queryTechnology = async (query: any) => {
127145
return await client.get(new dtos.QueryTechnology(query), { include: 'total' });
128146
};
129147

130148
export const getTechnologyTiers = async () => {
149+
if (technologyTiersCache) return technologyTiersCache;
131150
const request = new dtos.QueryTechnology();
132-
return (await client.get(request, {
151+
technologyTiersCache = (await client.get(request, {
133152
orderBy: 'tier,name',
134153
fields: 'id,name,tier,slug',
135154
jsconfig: 'edv'
136155
})).results;
156+
return technologyTiersCache;
137157
};
138158

139159
export const getPopularTechnologies = async (take: number = 50) => {
140-
const request = new dtos.QueryTechnology({
141-
orderBy: '-postsCount,-viewCount,-favCount',
142-
fields: 'id,name,slug,logoUrl,favCount,viewCount,postsCount',
143-
take,
144-
});
145-
return (await client.get(request, { jsconfig: 'edv' })).results;
160+
const all = await getAllTechnologies();
161+
const results = [...all];
162+
results.sort((a, b) => (b.postsCount ?? 0) - (a.postsCount ?? 0));
163+
return results.slice(0, take);
146164
};
147165

148166
export const getTechnologyPreviousVersions = async (slug: string) => {
@@ -154,17 +172,23 @@ export const getTechnologyPreviousVersions = async (slug: string) => {
154172
export const createTechnology = async (args: any, logo?: File) => {
155173
const request = new dtos.CreateTechnology();
156174
const body = toFormData({ ...args, logo });
157-
return (await client.postBody(request, body)).result;
175+
const result = (await client.postBody(request, body)).result;
176+
clearTechnologiesCache();
177+
return result;
158178
};
159179

160180
export const updateTechnology = async (args: any, logo?: File) => {
161181
const request = new dtos.UpdateTechnology();
162182
const body = toFormData({ ...args, logo });
163-
return (await client.putBody(request, body)).result;
183+
const result = (await client.putBody(request, body)).result;
184+
clearTechnologiesCache();
185+
return result;
164186
};
165187

166188
export const deleteTechnology = async (id: number) => {
167-
return await client.delete(new dtos.DeleteTechnology({ id }));
189+
const result = await client.delete(new dtos.DeleteTechnology({ id }));
190+
clearTechnologiesCache();
191+
return result;
168192
};
169193

170194
// ============================================

0 commit comments

Comments
 (0)