Server Cache in Node JS | JavaScript Node JS Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back to the channel today we'll take a look at server caching in noches we're going to use the express server and we're going to use a package called node cache to do our caching so the first step is to import our packages that we need so that's gonna be the express package and then you wanna import node fetch this is gonna let you make hd http call across the server and you want the actual caching package that's gonna be node cache there you go the next step is to actually instantiate an object for your cache that's gonna be let's just call it my cache let's create an object over here and you can pass it a bunch of options so the only one we care about right now is the ttl this means how long does your data stain the cache in seconds so if you insert a key in this cache it's going to stay there for 10 seconds and then just disappear okay so we're done with most of the packages the next thing we're gonna do is actually create our server and give it a port let's say three thousand let's save it uh yeah so the the http call we're gonna make is do this just to some json placeholder to get a bunch of to-do's we're gonna make the call without any caching and then we're gonna use caching to see what kind of performance improvement can you get so this is gonna be our url i'll just copy paste the url there you go so that just literally returns you a list of uh to-do's which are like mob data you go and let's create our first endpoint which is gonna be let's just call it to do's gonna take in a request response and for now let's just send back hello world we just want to see if this is working or not um once now that we have that let's actually start the server it's gonna be listening to our port and let's give it a call back so that we know that it's running an example let's just give it a template string here that's gonna be example server is running at port and then let's use the board number there you go uh let's kick off the server now so we're gonna just do uh yes so we're just gonna do one node index.js the server is running at port 3000 let's try to hit the server using postman so we can do we can just hit it there and [Music] so the end point is called posts for some reason we are getting an error let's see why oh they are called to-do's there you go and we're getting the hello world back so now instead of returning that hello world we want to actually return whatever we're getting from that to lose endpoint so what we want to do is just make the fetch call to the to-do's url and then once we do it the response comes back we're gonna turn the response into json and then when that comes back that is going to be the actual json object let's just send the json object back okay so let's see what we're doing here we're making a fetch request to our to-do's endpoint we're getting the response back and then turning it into json once it's turned into json we're just sending it back from our server so let us restart our server here and make the request now let's see what we're getting so we're using the same endpoint hit it and you see we're getting the whole list of to-do's and one thing to note here is that it's taking 140 milliseconds to get it if we hit it a few more times you can see it's gonna be 75 69 so it's always above 50 milliseconds and sometimes like even more than 100 so let's see how we can like reduce it so this is where the caching layer comes in so what we want to do is every time we get the list of to-do's back from this url we're gonna cache the data in memory and the next time there's a request that comes to our server instead of making these should be called to the json placeholder website we're going to just return it from our cache right so the first thing we want to do is put the data in our cache whenever we get it so that's going to be over here so instead of sending it immediately what we can do is we can do the my catch that set you give it a key for us it's going to be to do's and then let's give it the json right and then we can just return it so what this is doing is it's getting the json which is the list of to-do's it's sticking it into the cache uh with the key name to-do's so that if you look at the cache with the key name to do so you can get the json on the whole json list right so yeah let's try this out to see if anything broke or not first right we're gonna run the server again there you go and we're gonna hit the apa again okay so we're still getting it still is taking around more than 50 milliseconds we're averaging like 60 to 70 right nothing changed but now we know that every time we're getting the data we're throwing it in the cache so the next step would be now that we have it in the cache we want to return it from the cache if it is already there right remember we have the 10 second ttl uh so we the 10 second that the data isn't there we want to actually return it from the cache instead of making this fetch request so to do that what we can do is we can have a check here which checks if our cache actually has the key if it does you can just return it from the cache which is gonna be uh so you wanna if it's there you just wanna return it so you wanna destroy my cash.get and then the key right and if it is not there then you do this part which is make this typical there you go so what you're doing essentially is you're getting the request you're checking if it's already there in the cache if it is there in the cache then you're returning it from the cache by doing this get operation if it is not there you go into this else block and you make the api request get the response back stick it in the cache and then return the response so that the next time around the state is during the cache for how many every seconds you mentioned uh let's also just like put a few console logs so that we know which lines we're hitting so let's just do a castle log getting it from cache and here we're gonna just do a console log getting it from api right okay so let's restart our server there you go and then let's hit it now so the first time we're hitting it is still 92 millisecond now the data is here in the cache right so see the next ones are taking 19 22 20 20 very little and now hopefully 10 seconds has passed and now that we make the request again it should take longer like 60 to 70 millisecond because we are going to be hitting the json placeholder api again and as you can see it's taking 70 milliseconds if we look at the logs the first time it it got from the api and then the bunch of time it got the data from the cache and finally uh went 10 seconds past or whatever you put the ttl we're getting it from the api again uh so yeah and another thing i want to show you is regarding any kind of statistic you want to see about the cache so let's see how many times you're hitting the cash or if there are cash misses or uh whatever you want to know about the cash which is pretty straightforward to do so let's create another endpoint and let's call it stats request response again and we're just gonna uh we can just send it back i think we can just do a restaurant send and then we're just gonna do the cache and they have a function called get stats so let's try this out let's actually just send whatever response the stats function is giving us right i'm gonna save it i'm gonna restart the server and then let's take a look let's hit it so this is the to do some point right so we want to do this at some point see we're getting a bunch of statistics related to it uh you can see hits the number of times you actually got the data from the cache misses how many times you're missing it and then you have a bunch of other metadata related to related to the size so let's see how this hits number changes so let's hit our posts and phones our introduce end point 84 milliseconds so we're getting it from the api now we're getting it cash from the cash so it should be a cash hit cash hit again and cash it again let's check our status endpoint and yeah you're getting three hits over here uh that's pretty much all guys uh hopefully this is going to help you speed up your server by implementing cash in node.js if you like the video please leave a like if you have any comments put it down in the comment section below and i'm gonna get back to you whenever i can thank you so much i will see you in the next one bye
Info
Channel: Code With MD
Views: 2,258
Rating: 4.9444447 out of 5
Keywords: software development, web development, programming, javascript, node js, caching
Id: ipIGWZwxC7w
Channel Id: undefined
Length: 12min 3sec (723 seconds)
Published: Mon Feb 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.