Next.js Discord

Discord Forum

unstable_cache is too unstable for prod. what is the recommended way to cache a prisma fetch.

Answered
Plott Hound posted this in #help-forum
Open in Discord
Plott HoundOP
given the simple example how would approach caching this:
import prisma from '@/db/prisma';

export async function ListTasks() {
    const allTasks = await prisma.task.findMany();

    return (
        <div>
            {allTasks.map((task) => (
                <div key={task.id}>
                    <p>{task.title}</p>
                </div>
            ))}
        </div>
    )
}


bonus question: based on your choice of caching how would you revalidate that cache.

Thanks
Answered by joulev
what is the recommended way to cache a prisma fetch.
unfortunately, there isn't a better way than unstable_cache. but i am using it and i think it's pretty stable recently
View full answer

30 Replies

Answer
with unstable_cache you can also easily assign tags to revalidate at will
Plott HoundOP
thank you. i've also using it with unstable_cache for a few months but i occasionally get strange issues. I'm also using revalidate tag. I just thought i'd ask after seeing some of the discussion in #discussions
it's good enough for me for now but thought i'd ask if there were any good alternatives
yeah i would use the alternatives if they exist as well. but the only alternative i can imagine is that you make it a route handler and fetch it from the server component, which is even worse than using unstable_cache
but will require a redis server if you are hosting on serverless
@joulev yeah i would use the alternatives if they exist as well. but the only alternative i can imagine is that you make it a route handler and fetch it from the server component, which is even worse than using `unstable_cache`
Plott HoundOP
thanks. yes this is somewhat a callback to the first post i ever made in this discord weighing up the differences of fetching your own route handler vs unstable cache with a direct fetch in an RSC. and i believe you showed me the next faq page that explained it very well. so thanks again for the help, i'll keep my eye out for updates on unstable_cache and stick with what i have for now 🙂
@Ray https://github.com/epicweb-dev/cachified
Plott HoundOP
interesting! thanks for sharing, i'll try it out in a new project. have you had much experience using this? how did you find it?
im hosting on a server so thats not an issue
@Ray I use it with remix. the author is one of the remix core member
Plott HoundOP
cool. i'll give it a go and see how i get on. its tricky to compare since the issues i get with unstable are few and far between which makes them hard to log and isolate. looks cool though
@Ray https://github.com/epicweb-dev/cachified
cool, so this is a bring-your-own cache solution. will check this out, thanks for telling us about this project
yes and I found unstable_cache doesn't work well on other serverless hosting😆
actually the data cache
@Ray yes and I found `unstable_cache` doesn't work well on other serverless hosting😆
Plott HoundOP
interesting. perhaps my issues are in part due to me self hosting, i'll deploy it to vercel and monitor the differences
oh yeah it currently seems to be pretty coupled to vercel
you can have it running well for self-host via next start or vercel, but for it to work on other serverless hosts it will take some work at least
yes it works well with self hosting on a node server
Plott HoundOP
sorry to go slightly off topic, but since i have you both here - is using unstable_cache with user specific data a bad idea? eg adding the userId to the cache tag. im guessing its a terrible idea especially as the user base grows. is it common to just not cache anything user specific?
eg say i have 10,000 users if each of them have cached data then it would be a performance nightmare
the more you cache the more cost on caching you have to bear
regardless of what you cache
so decide wisely on whether to cache something or not
@Plott Hound eg say i have 10,000 users if each of them have cached data then it would be a performance nightmare
not sure how much it will cost :thinq:
Plott HoundOP
i see. either way the 'performance cost' could outweigh the benefit of caching in the first place
thank you guys, always learning loads from you. have a great day!! ❤️
I think you could not to cache it if the query is fast enought
Plott HoundOP
ok. im doing some dashboard stuff where i have lots of 'mini fetches' pushed down to the components that use them which are all suspense wrapped. it seems to work nicely but as always i dont want to be hitting the db more than i need to be
I would cache the user data too if I want to reduce the cost on db.