Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cached unfiltered data and applied filter to the cached data of key "search-query" #3141

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions backend/src/domain/search/searchTrainings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,39 +191,37 @@ export const searchTrainingsFactory = (dataClient: DataClient): SearchTrainings
const { page = 1, limit = 10, sort = "best_match" } = params;
const query = buildQuery(params);

const filteredCacheKey = `filteredResults-${params.searchQuery}`;
let filteredResults = cache.get<TrainingResult[]>(filteredCacheKey);
const unFilteredCacheKey = `filteredResults-${params.searchQuery}`;
let unFilteredResults = cache.get<TrainingResult[]>(unFilteredCacheKey);

// If filtered results are not in cache, fetch and filter them
if (!filteredResults) {
console.log(`Fetching and filtering results for query: ${params.searchQuery}`);
//const { allCerts, totalResults } = await fetchAllCertsInBatches(query);
// If unfiltered results are not in cache, fetch the results
if (!unFilteredResults) {
console.log(`Fetching results for query: ${params.searchQuery}`);
const { allCerts } = await fetchAllCertsInBatches(query);
const results = await Promise.all(
unFilteredResults = await Promise.all(
allCerts.map((certificate) =>
transformCertificateToTraining(dataClient, certificate, params.searchQuery)
)
);

// Apply filtering
filteredResults = await filterCerts(
results,
params.cip_code,
params.complete_in,
params.in_demand,
params.max_cost,
params.county,
params.miles,
params.zipcode,
params.format
);

// Cache the filtered results
cache.set(filteredCacheKey, filteredResults, 300); // Cache for 5 minutes
cache.set(unFilteredCacheKey, unFilteredResults, 300); // Cache for 5 minutes
} else {
console.log(`Cache hit for filtered results with key: ${filteredCacheKey}`);
console.log(`Cache hit for filtered results with key: ${unFilteredCacheKey}`);
}

// Apply filtering
const filteredResults = await filterCerts(
unFilteredResults,
params.cip_code,
params.complete_in,
params.in_demand,
params.max_cost,
params.county,
params.miles,
params.zipcode,
params.format
);

// Apply sorting to the cached filtered results
const sortedResults = sortTrainings(filteredResults, sort);

Expand Down