Caching ORM response in Next app dir

I've asked this question on the Next.js discord but wasn't able to get a solid answer, hoping someone here knows What is the recommended way to cache something like a prisma response? I have a page.tsx and in it I defined a revalidate variable and I have a getFirstPost function that fetches the first post:
export const revalidate = 3600; // 1 hour

const getFirstPost = async () => {
const latestPost = await prisma.post.findFirst({
orderBy: { createdAt: "desc" },
});

return latestPost;
};
export const revalidate = 3600; // 1 hour

const getFirstPost = async () => {
const latestPost = await prisma.post.findFirst({
orderBy: { createdAt: "desc" },
});

return latestPost;
};
in the page function I go
const latestPost = await getFirstPost();
const latestPost = await getFirstPost();
and then render the data. Even tho I defined the revalidate variable every time I refresh the page (on dev and on prod) I see prisma making a new query in console:
prisma:query SELECT `t3approuter`.`Post`.`id`, `t3approuter`.`Post`.`text`, `t3approuter`.`Post`.`createdAt`, `t3approuter`.`Post`.`updatedAt`, `t3approuter`.`Post`.`createdById` FROM `t3approuter`.`Post` WHERE 1=1 ORDER BY `t3approuter`.`Post`.`createdAt` DESC LIMIT ? OFFSET ?
prisma:query SELECT `t3approuter`.`Post`.`id`, `t3approuter`.`Post`.`text`, `t3approuter`.`Post`.`createdAt`, `t3approuter`.`Post`.`updatedAt`, `t3approuter`.`Post`.`createdById` FROM `t3approuter`.`Post` WHERE 1=1 ORDER BY `t3approuter`.`Post`.`createdAt` DESC LIMIT ? OFFSET ?
Does the cache get wiped on refresh or am I doing something wrong here?
3 Replies
hyperzone
hyperzoneOP2y ago
That's exactly what I do here and my point is that it's not working
Diogo
Diogo2y ago
could it bc youre using a a const and not a function like in the docs?
Want results from more Discord servers?
Add your server