Skip to content

Commit

Permalink
feat: add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Jan 5, 2024
1 parent f6f43bf commit 527c796
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 4 additions & 4 deletions components/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ const Search = () => {
try {
const universityParam = university;
const geParam = !ge.includes("GE") ? ge : ge.split(" ")[1];
const data = await queryDatabase(universityParam, geParam);
const courses = await queryDatabase(universityParam, geParam);

setCourses(data.courses);
setCourses(courses);
setLoading(false);
setError(false);

Expand Down Expand Up @@ -287,7 +287,7 @@ const Search = () => {
searchGE={ge}
/>
<div className="mt-8 flex flex-row gap-4 md:mt-16 md:gap-8">
<div className="hidden h-fit rounded-xl bg-bg_secondary p-8 xl:flex xl:flex-col">
<div className="bg-bg_secondary hidden h-fit rounded-xl p-8 xl:flex xl:flex-col">
<div className="mb-8 text-3xl font-medium">
Search Filters
</div>
Expand Down Expand Up @@ -317,7 +317,7 @@ const Search = () => {
</button>

<div className="flex items-center gap-4 md:flex-row">
<div className="hidden text-gray sm:flex">
<div className="text-gray hidden sm:flex">
Sort By:
</div>
<SortDropdown
Expand Down
19 changes: 17 additions & 2 deletions components/search/query-db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
export async function queryDatabase(university: string, ge: string) {
import { CollegeObject } from "./Search";

const cache: Record<string, CollegeObject[]> = {};

export async function queryDatabase(
university: string,
ge: string,
): Promise<CollegeObject[]> {
console.log(cache);
if (cache[university + ge]) {
return cache[university + ge];
}

const universityParam = encodeURIComponent(university);
const geParam = encodeURIComponent(ge);

Expand All @@ -11,7 +23,10 @@ export async function queryDatabase(university: string, ge: string) {
}

const data = await response.json();
return data;

cache[university + ge] = data.courses;

return data.courses;
} catch (error) {
console.error("Error:", error);
throw error;
Expand Down

0 comments on commit 527c796

Please sign in to comment.