Making Your APIs Blazingly Fast in .NET 8!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and this video I'm going to show you how you can use the amazing feature of output caching that we got in asp.net core in.net 7 and how you can actually use the built-in support we just got in.net 8 for redis to allow for distributed caching of the output this is something that we could technically do before however not everybody understands red is really really well and because there was no provider provided by Microsoft on how to do a type of caching nobody really did it with the output cache but now in dotnet 8 Microsoft themselves provide that type of redis provider and in this video I'm going to show you how you can use it and why they built it in such an amazing way if level of content and you want to see more make you subscribers for more training check out my courses on domtrain.com now before I move on I'd like to let you know what we just launched a brand new course on Dom train called getting started with solution architecture and that course is made by James istem like I have said in the past I only want to make courses myself on topics I am an absolute expert on and on this I just I'm not as good as James so I asked him to come over and offer that course for us James is a senior solution architect for AWS so his knowledge on the subject and his position in the biggest cloud provider in the world makes him the perfect educator for something like this the course is fantastic I've taken it myself and I cannot stress enough how good the quality of that course is I have said it in the past and I will say it again getting very good in solution architecture and understanding the subject will really Elevate you as a software engineer more than becoming better at the code itself because after some point you're gonna Plateau but solution architecture doesn't really have a limit and the moment you understand when to choose the right and for the right job you're going to be way more valuable into your company and your team Now to celebrate the launch I want to offer the first 400 of you a 15 discount code so you can use the code you see now on your screen and claiming 15 discount at checkout trust me this go really really fast so if you want to get the course get it sooner than the late now back to the video all right let me show what I have here I have assembly.net 8 API over here and it's a weather API but it's not your basic weather API the one you get from the template I've actually wired up open weather map which is a service you can use to actually pull the real current weather so now instead of creating five fake weathers I can run this API and I can go to post one to call it and if I say give me the weather for London as you're going to see we get 16 as a temperature which is Celsius and then Phil is like 16.53 so we have a way to get the current weather however the problem is that this service every time I call this endpoint will actually go and call the API service over here now what you can say at this point is hey Nick just add sort of a cache on this service level over here and be done with it and even though technically that is true that is not the most efficient level to actually add the level of caching the most efficient level for this specific use case is actually only the output itself whatever that response is at that point we can cash all all that the Jason's realized text everything and just put it somewhere and return it and up until.net 7 we could actually do that with output caching which is a new thing in.net 7 very very easily to do that all you need to do is go to the services and say builder.services.add output cache we won't really configure everything we're just gonna add this which adds the services behind the scenes and then after the redirection I'm going to say app.use output cache and once I have that I have two options I can either do that with the minimal API approach or the attribute approach what I can do with a minimal API approach is say dot after the request processing here and say cache output and since I haven't specified any defaults here then it's a bit unclear what exactly will be casting for how long but what I can do is I can use the options over here and I can say for example cache it or don't cache it by default if you do say cash output caching will happen and then we can say expire in time span and I can say for example here time span dot realistically you can say something like from minutes and say five minutes because how often does the weather change within a five minute Gap it doesn't really change and you can change that to 10 if it's more relevant for you I think five is a good medium here so within all of those five minutes no matter how many requests hit that endpoint only the first one will actually do any sort of processing and to prove that I'm gonna go here and I'm just gonna say debugging as you're going to see the moment I hit that end point we're going to get a request in here so the breakpoint is hit we're getting the weather and that weather now magically is cached but it's the output but at that stage it's only in memory just this single process but of course this can be problematic because if I scale out this application meaning I'm running many instances of this application to scale it and redirect the traffic then each instance will actually cache in memory its own output and you could have inconsistent weathers but just to prove that actually at this point this customer I go ahead and re hit that endpoint and as you can see no break point is really being hit I'm just returning from the cache that breakpoint is not touched super efficient super easy but if you do want to scale out the application you have a distributed system well you can't really do that there are ways to build your own cache provider so instead of having the built-in ones the in-memory one you can use one that has ready and I've actually done this in the past but it is very tricky to implement and you don't have to anymore because Microsoft actually made their own and the coolest thing about this is that it seems like this is written by Mark gravel who used to be one of the people working on the redis SDK we have that is from stack exchange before Stock Exchange got Acquired and he moved to Microsoft so it's from someone who has an insanely well understanding on the subject let's see how we can incorporate it into this application when I go to new it over here and I'm going to search for microsoft.extensions.cashing and then stack Exchange change radius not service exchange rate is stack exchange rate is and I'm going to use the latest RC version over here so let's go ahead and install that I mean rc1 of.net8 at this point so now with that in place all I need to do to incorporate it is say add stack exchange output cache and I can of course configure it as well so you have configuration options here for example a configuration string configuration options an instance name and you can even provide your own multiplexer so you might have seen people provide multiplexers here so you would have like an async and then connection and then connect async you would have waited and I'm going to run release locally so the end point would be localhost and then 6379 which is default but I can do something like this or I could very much not do any of this and say for example instance name and give this instance the weather API as the name and then configuration and pass that as a string so I can say localhost and then the endpoint so both of these options will actually work depends on how much control you need especially around the multiplexer factory so once we have that then we don't really need to do anything I'm going to go ahead and just stick a breakpoint here and I'm going to run redis now I have a Docker installed so I'm going to run redis in Docker and I'm going to go here in the console and use the official redis stack server and default ports default everything I'm just going to save that for the image redis is now running and if I want to explore radius and just see what's in there I'm using another redis desktop manager which if I refresh you can see all the radius options over here currently we have nothing no databases because well no keys are there so let's go ahead and run this API now and see what happens So currently nothing in the cache and if I go ahead and I say give me the weather for London then as you're going to see we're going to hit that endpoint we're gonna get the weather for London but then if I try to call it again nothing happens no endpoint is being hit and that's because the response now is cached in a distributed way into red this so if I go here and I say refresh this now you can see the weather API which is the prefix of the instance name we provided then you can see another prefix over here and then you have the exact request that is being called so in this case it is localhost 5001 weather and then City London so that query string parameter is also part of the key which means that if I go ahead and I say give me the weather for London I'm gonna keep getting that cash value which is cashed for in my case five minutes but if I say hey how about you give me the weather for Milan Italy then if you do that then we will hit the endpoint that's because the query string parameter is part of the key and now those keys are separately cached in a very nice way so if you want to see that cast whether or that cast weather the separate very very nice now this is not the only cool thing about this because one of the hardest thing about cash is actually cash invalidation so the way um this type of caching does it is by allow you to provide tags and that was actually one of the most complicated things to do if you were to write your own release provider but now all you want to do is go here and chain this as a tag and I'm going to tag this as a weather cache for example so whenever I say add this into the cache also give it a name and say that this is weather related basically and now to invalidate it you wouldn't really have a separate endpoint for invalidation it would be part of your process but just to show you how it would work I'm gonna create an inval at endpoint and I'm going to say async here and pass down the I output Cache Store over here so I'm going to say store and I'm gonna say return results dot okay over here but I'm also going to say a weight store dot evict by tag and what I want to evict here is a by tag weather and this also needs me to provide the cancellation token because of course it does but to actually show you how the invalidation will work I need to add these entries in a way they're Tagged so I have to go ahead and just delete them first so let's go ahead and clear the cache this one and this one and re-add them so I'm gonna call Milan we're gonna hit that end point and then I'm gonna do London as well we're gonna hit that end point we're going to Cache it and now if I go to redis and I refresh you're going to see two more entries you're gonna see this one over here which sort of helps with the tags and this one over here would keep track of entries related to that tag so the tag here is the weather this is a z set for redis if you know what that is or it's basically set and then you have the individual entries to know what to invalidate so now what I can do is I can go to post one and I can say invalidate anything weather related and the moment I do that as you're going to see my cache is now cleared I still have this entry because it's just long term entry but all the other ones do get removed so if I go and I say give me the weather for London again I'm going to get it how cool is that and how easy is it and everything still works so if you want to have things like multiple tags expiry policies you want to vary by value you want to verify a header host query anything all that still works in any case output caching is an amazing feature it's wildly different than the response caching we used to have and by the way if you want to use something like this in for example controllers then you can use the output cache attribute which works both here in minimal apis but also controllers highly recommended feature caching can really just fix a lot of performance problems you might have in your application and with redis it is just very very easy to deal with but now I learn from you are you using output caching or are you using something else leave a comment down below and let me know thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 60,320
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, .net 8 redis, redis cache, redis output cache, redis response cache, output cache, response cache, api response cache, redis asp.net core, Making Your APIs Blazingly Fast in .NET 8!
Id: _bg5dGnudPs
Channel Id: undefined
Length: 11min 44sec (704 seconds)
Published: Thu Aug 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.