diff --git a/webapp/src/app/modules/university-ranking/services/trend-data.service.ts b/webapp/src/app/modules/university-ranking/services/trend-data.service.ts index 106bf141..626102f3 100644 --- a/webapp/src/app/modules/university-ranking/services/trend-data.service.ts +++ b/webapp/src/app/modules/university-ranking/services/trend-data.service.ts @@ -26,7 +26,7 @@ export class TrendDataService { private readonly apiService = inject(UniversityRankingsService); private readonly personsApiService = inject(PersonsService); - // Soft cache signals + // Cache signals readonly cachedResponse = signal(null); readonly cachedParams = signal(null); @@ -40,44 +40,45 @@ export class TrendDataService { readonly isTopTopicsLoading = signal(false); readonly topTopicsError = signal(false); + private readonly cacheMap = new Map(); + + private getCacheKey(params: TrendRequestParams): string { + return JSON.stringify({ + searchType: params.searchType, + fuzzy: params.fuzzy, + topics: [...params.topics].sort(), + keywords: params.keywords ? [...params.keywords].sort() : [], + insts: (params.institutions || []).map(cleanEntityId).sort(), + authors: (params.authors || []).map(cleanEntityId).sort(), + countries: (params.countries || []).sort(), + regions: (params.regions || []).sort(), + }); + } + /** - * Updates soft cache with response and params from resolver or component. + * Updates cache map with response and params from resolver or component. */ updateCache(response: TrendResponse, params: TrendRequestParams): void { + const key = this.getCacheKey(params); + this.cacheMap.set(key, response); this.cachedResponse.set(response); this.cachedParams.set(params); } /** - * Checks if requested params can be satisfied by cached response. + * Checks if exact requested params can be satisfied by cached response. */ canReuseCache(newParams: TrendRequestParams): boolean { - const currentResp = this.cachedResponse(); - const currentParams = this.cachedParams(); - if (!currentResp || !currentParams) return false; + const key = this.getCacheKey(newParams); + return this.cacheMap.has(key); + } - if (currentParams.searchType !== newParams.searchType) return false; - if (currentParams.fuzzy !== newParams.fuzzy) return false; - - // Topics check - const currentTopics = [...currentParams.topics].sort().join(','); - const targetTopics = [...newParams.topics].sort().join(','); - if (currentTopics !== targetTopics) return false; - - // Check if new entities are a subset of cached entities - const currentInsts = new Set( - (currentParams.institutions || []).map(cleanEntityId), - ); - const targetInsts = (newParams.institutions || []).map(cleanEntityId); - if (!targetInsts.every((id) => currentInsts.has(id))) return false; - - const currentAuthors = new Set( - (currentParams.authors || []).map(cleanEntityId), - ); - const targetAuthors = (newParams.authors || []).map(cleanEntityId); - if (!targetAuthors.every((id) => currentAuthors.has(id))) return false; - - return true; + /** + * Retrieves exact cached response for requested params. + */ + getCachedResponse(newParams: TrendRequestParams): TrendResponse | null { + const key = this.getCacheKey(newParams); + return this.cacheMap.get(key) ?? null; } /** diff --git a/webapp/src/app/modules/university-ranking/uni-trend-parent/uni-trend-parent.component.ts b/webapp/src/app/modules/university-ranking/uni-trend-parent/uni-trend-parent.component.ts index 1b7d0cd3..81aa247e 100644 --- a/webapp/src/app/modules/university-ranking/uni-trend-parent/uni-trend-parent.component.ts +++ b/webapp/src/app/modules/university-ranking/uni-trend-parent/uni-trend-parent.component.ts @@ -42,6 +42,7 @@ import { InstitutionsService, PersonsService, OrganizationService, + Author, } from '../../../api'; import { CredentialService } from '../../../shared/services/credential.service'; import { TopicChipComponent } from '../../../shared/ui/topic-chip/topic-chip.component'; @@ -197,7 +198,7 @@ export class UniTrendParentComponent implements OnInit { readonly locationQuery = signal(''); readonly locationSearchResults = signal([]); readonly authorSearchQuery = signal(''); - readonly authorSearchResults = signal([]); + readonly authorSearchResults = signal([]); readonly authorInstSearchQuery = signal(''); readonly authorInstSearchResults = signal( [], @@ -1134,6 +1135,7 @@ export class UniTrendParentComponent implements OnInit { if (!exists) { this.hasUserClearedInstitutions.set(false); this.comparisonInstitutions.set([...current, { id: cleanId, name }]); + this.isTrendLoading.set(true); this.updateUrlParams(); } this.searchQuery.set(''); @@ -1150,6 +1152,7 @@ export class UniTrendParentComponent implements OnInit { this.hasUserClearedInstitutions.set(true); } this.comparisonInstitutions.set(updated); + this.isTrendLoading.set(true); this.updateUrlParams(); } @@ -1174,6 +1177,7 @@ export class UniTrendParentComponent implements OnInit { this.locationQuery.set(''); this.locationSearchResults.set([]); this.loadTrendSuggestions(); + this.isTrendLoading.set(true); this.updateUrlParams(); } @@ -1190,6 +1194,7 @@ export class UniTrendParentComponent implements OnInit { ); } this.loadTrendSuggestions(); + this.isTrendLoading.set(true); this.updateUrlParams(); } @@ -1219,7 +1224,7 @@ export class UniTrendParentComponent implements OnInit { next: (res) => { const current = this.comparisonAuthors(); const updated = [...current]; - (res.results || []).forEach((author: any) => { + (res.results || []).forEach((author: Author) => { const shortId = cleanEntityId(author.id); if ( shortId && @@ -1236,6 +1241,7 @@ export class UniTrendParentComponent implements OnInit { } this.comparisonAuthors.set(updated); this.trendDataService.isSuggestionsLoading.set(false); + this.isTrendLoading.set(true); this.updateUrlParams(); }, error: (err) => { @@ -1254,6 +1260,7 @@ export class UniTrendParentComponent implements OnInit { if (!current.some((a) => cleanEntityId(a.id) === shortId)) { this.hasUserClearedAuthors.set(false); this.comparisonAuthors.set([...current, { id: shortId, name }]); + this.isTrendLoading.set(true); this.updateUrlParams(); } } @@ -1268,6 +1275,7 @@ export class UniTrendParentComponent implements OnInit { this.hasUserClearedAuthors.set(true); } this.comparisonAuthors.set(updated); + this.isTrendLoading.set(true); this.updateUrlParams(); } diff --git a/webapp/src/app/resolvers/uni-trend.resolver.ts b/webapp/src/app/resolvers/uni-trend.resolver.ts index e94170da..e48f81de 100644 --- a/webapp/src/app/resolvers/uni-trend.resolver.ts +++ b/webapp/src/app/resolvers/uni-trend.resolver.ts @@ -184,10 +184,10 @@ export const uniTrendResolver: ResolveFn = ( searchType, }; - // Check soft cache + // Check exact parameter keyed cache let trendObs$: Observable; if (trendDataService.canReuseCache(currentParams)) { - trendObs$ = of(trendDataService.cachedResponse()); + trendObs$ = of(trendDataService.getCachedResponse(currentParams)); } else { trendObs$ = apiService .getTrendApiInseServiceUniversityRankingsTrendGet(