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

feat: add cache #40

Merged
merged 5 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
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
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
25 changes: 23 additions & 2 deletions components/search/query-db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
export async function queryDatabase(university: string, ge: string) {
import { CollegeObject } from "./Search";

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

export async function queryDatabase(
university: string,
ge: string,
): Promise<CollegeObject[]> {
const cacheKey = university + ge;

if (cache[cacheKey] && cache[cacheKey][0]) {
const [cachedDate, cachedData] = cache[cacheKey];

// If not older than 30 minutes, return cached courses
if ((new Date().getTime() - cachedDate.getTime()) / (1000 * 60) <= 30) {
return cachedData;
}
}

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

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

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

cache[cacheKey] = [new Date(), data.courses];

return data.courses;
} catch (error) {
console.error("Error:", error);
throw error;
Expand Down
13 changes: 7 additions & 6 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/** @type {import('next').NextConfig} */

const runtimeCaching = require("next-pwa/cache");
const withPWA = require('next-pwa')({
dest: 'public',
const withPWA = require("next-pwa")({
dest: "public",
register: true,
skipWaiting: true,
runtimeCaching
})
runtimeCaching,
disable: process.env.NODE_ENV === "DEVELOPMENT",
});

module.exports = withPWA({
reactStrictMode: false
})
reactStrictMode: false,
});