changed caching to Parameter Keyed caching. Rest is handled by resolver and e-tags
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/head This commit looks good
This commit is contained in:
@@ -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<TrendResponse | null>(null);
|
||||
readonly cachedParams = signal<TrendRequestParams | null>(null);
|
||||
|
||||
@@ -40,44 +40,45 @@ export class TrendDataService {
|
||||
readonly isTopTopicsLoading = signal<boolean>(false);
|
||||
readonly topTopicsError = signal<boolean>(false);
|
||||
|
||||
private readonly cacheMap = new Map<string, TrendResponse>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+10
-2
@@ -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<string>('');
|
||||
readonly locationSearchResults = signal<AutocompleteLocationItem[]>([]);
|
||||
readonly authorSearchQuery = signal<string>('');
|
||||
readonly authorSearchResults = signal<any[]>([]);
|
||||
readonly authorSearchResults = signal<Author[]>([]);
|
||||
readonly authorInstSearchQuery = signal<string>('');
|
||||
readonly authorInstSearchResults = signal<AutocompleteResponseResultsInner[]>(
|
||||
[],
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -184,10 +184,10 @@ export const uniTrendResolver: ResolveFn<UniTrendResolvedData> = (
|
||||
searchType,
|
||||
};
|
||||
|
||||
// Check soft cache
|
||||
// Check exact parameter keyed cache
|
||||
let trendObs$: Observable<TrendResponse | null>;
|
||||
if (trendDataService.canReuseCache(currentParams)) {
|
||||
trendObs$ = of(trendDataService.cachedResponse());
|
||||
trendObs$ = of(trendDataService.getCachedResponse(currentParams));
|
||||
} else {
|
||||
trendObs$ = apiService
|
||||
.getTrendApiInseServiceUniversityRankingsTrendGet(
|
||||
|
||||
Reference in New Issue
Block a user