Cache clubs and tournaments for 6 hours at a time

This commit is contained in:
Timothy Armes
2023-10-22 19:14:48 +02:00
parent 30da984a97
commit 1084eea6b1
2 changed files with 24 additions and 5 deletions
+12 -2
View File
@@ -1,4 +1,4 @@
import { groupBy } from "lodash"; import { unstable_cache } from "next/cache";
import clientPromise from "@/lib/mongodb"; import clientPromise from "@/lib/mongodb";
import { Club, ClubData } from "@/types"; import { Club, ClubData } from "@/types";
@@ -38,6 +38,16 @@ const getClubs = async () => {
}; };
export default async function Clubs() { export default async function Clubs() {
const clubs = await getClubs(); const clubs = await unstable_cache(
async () => {
const data = await getClubs();
return data;
},
["clubs"],
{
revalidate: 60 * 60 * 6,
},
)();
return <ClubsDisplay clubs={clubs} />; return <ClubsDisplay clubs={clubs} />;
} }
+12 -3
View File
@@ -1,5 +1,6 @@
import { differenceInDays, isSameDay, parse } from "date-fns"; import { differenceInDays, isSameDay, parse } from "date-fns";
import { groupBy } from "lodash"; import { groupBy } from "lodash";
import { unstable_cache } from "next/cache";
import clientPromise from "@/lib/mongodb"; import clientPromise from "@/lib/mongodb";
import { TournamentData } from "@/types"; import { TournamentData } from "@/types";
@@ -8,8 +9,6 @@ import { errorLog } from "@/utils/logger";
import TournamentsDisplay from "./TournamentsDisplay"; import TournamentsDisplay from "./TournamentsDisplay";
export const revalidate = 3600; // Revalidate cache every 6 hours
const getTournaments = async () => { const getTournaments = async () => {
try { try {
const client = await clientPromise; const client = await clientPromise;
@@ -145,6 +144,16 @@ const getTournaments = async () => {
}; };
export default async function Tournaments() { export default async function Tournaments() {
const tournaments = await getTournaments(); const tournaments = await unstable_cache(
async () => {
const data = await getTournaments();
return data;
},
["tournaments"],
{
revalidate: 60 * 60 * 6,
},
)();
return <TournamentsDisplay tournaments={tournaments} />; return <TournamentsDisplay tournaments={tournaments} />;
} }