From f83dcafbf7b5239fb93891c312d199ff24f3f8f7 Mon Sep 17 00:00:00 2001 From: denis0001-dev Date: Mon, 10 Nov 2025 21:50:17 +0300 Subject: [PATCH] Cache user similarity to reduce requests --- frontend/src/core/api/profileApi.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/frontend/src/core/api/profileApi.ts b/frontend/src/core/api/profileApi.ts index f16f03b..5a25935 100644 --- a/frontend/src/core/api/profileApi.ts +++ b/frontend/src/core/api/profileApi.ts @@ -171,22 +171,40 @@ export async function verifyUser(userId: number, token: string): Promise<{verifi } } +/** + * In-memory cache for user similarity results + * Key: userId, Value: similarity result + */ +const similarityCache = new Map(); + /** * Checks if a user is similar to any verified user + * Results are cached in memory to avoid redundant API calls */ export async function checkUserSimilarity(userId: number, token: string): Promise<{isSimilar: boolean, similarTo?: string} | null> { + // Check cache first + if (similarityCache.has(userId)) { + return similarityCache.get(userId) ?? null; + } + try { const response = await fetch(`${API_BASE_URL}/user/check-similarity/${userId}`, { headers: getAuthHeaders(token) }); + let result: {isSimilar: boolean, similarTo?: string} | null = null; if (response.ok) { - return await response.json(); + result = await response.json(); } - return null; + // Cache the result (even if null/error) + similarityCache.set(userId, result); + return result; } catch (error) { console.error('Error checking user similarity:', error); - return null; + const result: null = null; + // Cache null result to avoid retrying on errors + similarityCache.set(userId, result); + return result; } }