Want to know how to use React’s new cache() function in Next.js and why it's a game-changer for performance? In this video, I’ll explain what cache() does, why it’s important for server-side rendering, and how you can implement it to avoid unnecessary DB/API calls. We’ll cover everything from basics to real-world Next.js integration. Perfect for React 18+ and App Router users! ------------------------------------------------------------------------ 🚀 Boost Your Skills with these Pre-Requisite Videos:
⌛TIMELINE⏳ 0:00 Intro to React Cache in Next.js 0:16 What is React Cache and How it Works 3:00 Very Important Tips about Caching 3:50 Why Do We Need React Cache 6:30 Creating Function to Get DB Data 8:15 Implementing React Cache in Our Next.js App 10:18 Final Tips & Common Mistakes to Avoid
I believe this is a very important feature which was introduced in react version 18
So what is it? I will tell you.
Basically, this is the part of react which you can use in next as well.
So guys, what is this CAS? You can see it here very easily.
This is a function which was introduced in react 18. I have already told you about it.
What does it allow? It allows you to memoize. It means to store the result of a function based on its argument.
So it doesn't run again if called with the same argument.
I will tell you the simple part of it. I will also tell you the practical part of it.
Let's imagine this is your component. This is your application.
Assume that you are requesting the database 10 times that I want a data.
And your query is the same.
Now, if you take the traditional approach, the server will get the data 10 times.
Basically, we get the data from the database and we show it in the UI.
But, if you use react CAS, then what will happen?
For the very first time, you can see this carefully.
For the first time, we will get the data from the database.
And what will we do with the result?
We will store the result in the CAS.
We will store the data in the CAS.
Whichever data you got for the first time, we will add it in the CAS.
Now, how many more requests do we have?
Like we had 10, so we will say 9 more requests.
There are still more, right?
There are still 9 more requests.
So, this is the beauty.
If we use CAS, then the remaining 9 requests will not be requested to the database.
Whatever data is there, we will get it directly from the CAS.
So, this is a very big advantage.
You can't even imagine.
Let's assume that traditionally, the database would be requested 10 times.
But, let's assume that your first request,
Let's assume that your first request is 5 seconds long.
Let's assume that your second request is 5 seconds long.
Every time it is 5 seconds, how many seconds did you spend for 10 requests?
50 seconds.
But, if you use CAS,
For the first time, your first request will take 5 seconds.
But, after that, all the requests,
The argument is the same, right?
So, it will not call.
So, it will directly give you the data from CAS.
Like, 5 seconds.
In 0 seconds, you will get it.
So, on one side, you have 50 seconds.
On the other side, you have only 5 seconds.
So, which is more advantageous?
Obviously, CAS, right?
So, let's make it practical.
And here you can see,
This is particularly useful on the server
to avoid repeated data fetching
for the same input,
especially during the SSRN
or RSE rendering.
And here, another important thing,
CAS works only in the server components
or in server environments
like Next.js app router.
And now, we are working on the app router.
Keep this in mind.
This is very important.
Even here, it is said,
CAS is only for use with React server component.
And don't say,
what is React server component?
Many people have doubts about it.
So, I would like to tell you,
the difference between React server component
versus client component is very important.
This is the most important.
And yesterday, we saw ISR.
And here, you can see,
static versus dynamic page.
So, on this page,
we will follow the code I wrote.
And we will understand,
how React CAS works.
How this function makes our life easier.
I am telling you,
this is a very important video.
If you don't have the source code,
I will provide the link in the description
and comment section.
You can check it out in every video.
I will also add the PPT.
If I run this particular part,
you can see,
that is your rendering,
and not static,
but dynamic.
If I do this,
I can see the dynamic data.
This is very good.
If I run this,
you can see,
Fetching Doctors console.
This is very important.
This will clear all your doubts.
Because, I have written Fetching Doctors.
Right?
This particular part,
where we are looping the doctor,
why don't I make a separate component
and name it,
let's say, Doctor List.
I am going to name it,
Doctors,
right?
I am not going to make it fancy.
It's your doctor list.
Let's go here.
We have a return.
Obviously, we have a fragment.
Let's paste it.
No doubt, I have to destructure it.
That is your doctors.
So that I can map it here.
You can see,
in the same page,
I have two components.
First is this.
Let's save it.
I am going to refresh it.
No problem.
I will get the data as it is.
If we see here,
I can see Fetching Doctors.
Good.
But,
what if I copy and paste
the same piece of code here.
We don't have a big project.
But, I want to clear the concept.
So,
same piece of data.
I am calling two places.
You can see here,
Doctor has already been.
Let me change the name.
I should get the data.
But, if you see here,
Fetching Doctors.
Wait a second.
Let me refresh it again.
Now, this is perfect.
Now, I can see Fetching Doctors
and Fetching Doctors.
You can say with a guarantee,
this query is running for the first time.
And, this query is running for the second time.
Both are same.
So, I can save my time.
If I get the data for the first time,
I can store the result on cache.
And, next time,
if I am firing the same query,
I can take the value from cache.
Right.
Let me tell you how to do that.
Before doing that, we can do it better.
Why not?
We are writing the same piece of code in both places.
Let's make it a function.
Right.
Here, you can do one thing.
Simple.
I am removing it from here.
And, I am pasting it here.
Doctors don't need to do that much.
And, by the way,
I am removing it from here.
Right.
Here, you have to make it async.
No matter what.
Obviously, whenever you call a function,
or define a function,
you have to mention what you want to return.
Whenever a function is called,
I want to return the data of doctors.
Now, wherever it is being used,
where will it be used?
Obviously, over here.
We have a doctors equal to
This is your function.
And, by the way, it is returning a promise.
So, here also, we need to go with await.
Copy it.
Come here.
And, paste it.
It is the same thing.
We are making further improvements.
Now, we have to see
whether it is called this time also.
Because, this function is called.
It means that you will see it on console.
Fetching doctors.
And, one more thing I want to tell you.
Warning.
I guess, we don't need doctors here.
Let's save the controller.
Let's see what happens.
Alright. See, I got doctors.
And, here.
Let's go here.
We are getting fetching data two times.
It means that
it is the same query.
Get all doctors.
It is the same.
Here also, it is the same.
Now, how does react cache work here?
See, what was written here.
If you pass the same argument,
it won't run again and again.
It means that it is simple.
See, what should we do for that?
There is nothing to do for that.
Here, we need to import cache.
See, from your react.
This is it.
You have to get this cache function.
And, the function which you think
is being repeated again and again.
And, you want it to be requested only once.
After that, all the data will be stored on cache.
And, that, in our case,
is nothing but this function.
So, whatever part you see here,
the async part.
Here, you have to use cache.
Start the parenthesis.
In this way,
you have to wrap everything inside it.
This is it.
Here, you have to write cache in this way.
And, you have to wrap that function inside it.
Which you think is being repeated again and again.
This is it.
Now, if I go here.
Let me refresh it once.
And, now I go here.
See, this is it.
How did you like it?
See, you can see fetching doctors
only once.
What does it mean?
It means that, this function,
which was run a while ago,
when this component was run for the first time,
it called this function.
Fetching doctors came to me.
Let's go further down.
Again, we have a doctor list component.
I am calling the same function in that too.
But, this time,
this function was not called in this component.
This is what I told you in the beginning.
This is a component.
And, you have to make 10 requests in it.
How many requests?
10 requests.
So, if you use cache,
here we have a cache.
The request will go to the database for the first time.
But, after that,
the output will be stored on the cache.
And, after that,
it doesn't matter how many times it is used.
It is stored in the cache.
And, from there,
the second argument,
it got the data from here.
How awesome is this?
How much time will you save here?
And, one more thing to keep in mind here is that
the moment you refresh,
for the very first time,
the request will go to the server.
And then, again,
the second time,
the request doesn't need to go.
So, this is the beauty of your react cache,
which was introduced after version 18,
which only works in the server component.
And, it is very useful.
You can see.
It is very important.
Believe me.
We saw suspense, react cache,
static vs dynamic page.
Yesterday, we saw ISR.
So, isn't it awesome?
You are getting so many powers.
I hope,
you liked this video.
This is the thing.
For the very first time,
the request will go to the database.
Once you get the data,
you know that you have to get the same data everywhere.
So, why should we request again and again?
Just keep showing the data from there.
You saw,
the fetching request was coming twice.
Now, I am getting it only once.
Similarly, I will use it in different places.
Still, I will get the data fetching only once.
Rest, they will get the data of cache everywhere.
So, yeah, guys, this is it.
That's all for today's video.
I hope you liked the video a lot.
And, whoever had doubts regarding this,
all the doubts will be cleared in today's video.
Still, if you have any doubt,
you know that this time,
our next series is like
one of the most awesome series.
Because, I am replying to all your comments.
I don't want that you watch the video
and your doubt remains with you.
No, you reach to me.
I will clear it.
Guys, source code and notes are free.
So, if I do this much, you can also support me.
You don't have to do anything.
You just have to like the video.
Comment and tell me what you like and what you don't like.
And, please share it with your friends.
And, our next target is 8 lakh subscribers.
If you haven't subscribed yet,
please do it.
Till then, guys, thank you so much.
Take care.