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/isr #727

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
121 changes: 60 additions & 61 deletions apps/web/components/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { MCQQuestion, Prisma, Problem, Track, TrackProblems } from "@prisma/client";
import db from "@repo/db/client";
import { cache } from "../../../packages/db/Cache";
import { revalidateTag, unstable_cache } from "next/cache";

interface Tracks extends Track {
problems: { problem: Problem }[];
Expand Down Expand Up @@ -156,52 +157,48 @@ export async function getTrack(trackId: string) {
}
}

export async function getAllTracks() {
const value = await cache.get("getAllTracks", []);
if (value) {
const data: AllTracks[] = value.map((track: Tracks) => ({
...track,
problems: track.problems.map((problem: { problem: Problem }) => ({ ...problem.problem })),
}));
return data;
}
try {
const tracks = await db.track.findMany({
where: {
hidden: false,
},
include: {
problems: {
select: {
problemId: true,
problem: true,
export const getAllTracks = unstable_cache(
async () => {
try {
const tracks = await db.track.findMany({
where: {
hidden: false,
},
include: {
problems: {
select: {
problemId: true,
problem: true,
},
orderBy: [
{
sortingOrder: "desc",
},
],
},
orderBy: [
{
sortingOrder: "desc",
categories: {
select: {
category: true,
},
],
},
categories: {
select: {
category: true,
},
},
},
orderBy: {
createdAt: "asc",
},
});
await cache.set("getAllTracks", [], tracks);
return tracks.map((track: any) => ({
...track,
problems: track.problems.map((problem: any) => ({ ...problem.problem })),
}));
} catch (e) {
console.error(e);
return [];
}
}
orderBy: {
createdAt: "asc",
},
});
await cache.set("getAllTracks", [], tracks);
return tracks.map((track: any) => ({
...track,
problems: track.problems.map((problem: any) => ({ ...problem.problem })),
}));
} catch (e) {
console.error(e);
return [];
}
},
["tracks"],
{ revalidate: parseInt(process.env.CACHE_EXPIRE || "1800", 10), tags: ["tracks"] }
devsargam marked this conversation as resolved.
Show resolved Hide resolved
);

export async function createTrack(data: {
id: string;
Expand Down Expand Up @@ -251,6 +248,8 @@ export async function createTrack(data: {
});
});
}
revalidateTag("tracks");
revalidateTag("categories");
return track;
} catch (e) {
return new Error("Failed to create track");
Expand Down Expand Up @@ -314,25 +313,25 @@ export async function updateTrack(
}
}

export async function getAllCategories() {
const value = await cache.get("getAllCategories", []);
if (value) {
return value;
}
try {
const categories = await db.categories.findMany({
select: {
id: true,
category: true,
},
distinct: ["category"],
});
await cache.set("getAllCategories", [], categories);
return categories;
} catch (e) {
return [];
}
}
export const getAllCategories = unstable_cache(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit

import { unstable_cache as cache } from "next/cache";

would be better if we migrate in future

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devsargam cache variable was already there so i used different alias (nextCache)

async () => {
try {
const categories = await db.categories.findMany({
select: {
id: true,
category: true,
},
distinct: ["category"],
});
await cache.set("getAllCategories", [], categories);
return categories;
} catch (e) {
return [];
}
},
["categories"],
{ revalidate: parseInt(process.env.CACHE_EXPIRE || "1800", 10), tags: ["categories"] }
);

export async function getAllMCQs() {
const value = await cache.get("getAllMCQs", []);
Expand Down