Caching in ASP.NET C# - Memory Caching is AMAZING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks it's Yannick from tutorial CU and in this video we're going to talk about in-memory caching in asp.net core and yeah let's let's just get right into it [Music] here we got a huge huge product list I got 2 000 products right here right and guess what chupt created them for me right so I just told them well create an SQL query with some random product data and I executed it and now we're done with that so here we got a huge product list and well guess what it takes some time to load them from the database right and since we're good developers we always want to provide our users the very best experience so caching is a great way to save loaded data in the application's memory on the server side in that scenario right here and when another user requests the same data it will get loaded super fast because it's cached in the memory of the server so right now everything is cached already we will just dig deeper into how it works exactly just in a second but if I now clear the cache and the page automatically reloads for sure the data will get fetched from the database so let's take a look at our application right now I got some pretty nice locks right here you can see that after I clicked on clear cache here we got it right on screen it got loaded from the database again and it took in total 189 milliseconds now right now everything is cached again so when I simply reload the page and now go back to our logs you can see that it only took 13 milliseconds right and it differs sometimes it's two milliseconds right now I got a screen recording program and all of that running and sometimes it's 10 milliseconds but it's definitely way faster it's so much faster well just think of it like 10 times 50 times or even 200 times faster well loading from Cache is definitely something that you don't want to miss so let's find out how you can add it to your asp.net core application and I can promise you it's way easier than you think but first of all make sure to subscribe to our channel so that you no longer miss any upcoming.net related videos because we are here to help you become a better developer and for sure you are interested in that also smash the video's like button if you love asp.net and if you take your c-sharp Curry series check out our c-sharp progress Academy it's a self-paced online course turning you into a full stack c-sharp web developer with angular asp.net core and I can promise it's the fastest way on how you can progress as a c-sharp developer you can find the link in the description below or popping up right now at the top right corner now let me just shut down the application we will get to that just in a second again now let's get started I will close everything here now let's talk about caching so first of all the good news is as long as you use kind of MVC or any of the other templates on asp.net core that gets provided you can just use the I memory cache it automatically gets injected via the dependency injection now if you have no clue about the dependency injection check out our video on that topic since it's really important if you are a.net Developer you have to understand how Services get injected into your applications great now afterwards what we have right here is a simple very simple MVC application we have an application DB context right so nothing special inside here we got the products table and the product consists of ID name and price but that doesn't really matter it could be really anything great so let me just close that again I just want to show you the code here now what we have right here is an i memory cache so that's quite important you need that and as I said you don't have to install any additional package it gets automatically provided now as the name already indicates it is saving the cached items to the applications memory on the service side not on the user's side we're not talking about any kind of user cookie no we are saving the products inside of the server memory cache so that let's say user a is requesting the products now it gets saved on the server now user B wants to get them to and the cache data just gets transferred to the user right so just keep that in mind the cache data is on the server not on the client side now here we got a cache key well that's just kind of like a dictionary key right so you have any specific key for any specific value we could have a user cache Key Products cache key whatever invoices cache key maybe not the best idea but it would work so you have a key that key is connected to cache data well maybe now here we got our Constructor and that Constructor is just taking that I memory cache from the dependency injection so that we got an instance from it awesome and now let the magic happen right here we got our index page which is just as I said it's an MVC application it just shows our index page that huge list of products so what I do right here is just creating a stopwatch so that we can measure the well actual time that's passing from starting the request to finishing it right now here is where the real magic is happening right we are using our cache object we are trying to get value you know that try get value right it's a method that you use when you work with dictionaries now we submit the key here so we say Hey try to get a value from the cache using the cache key which is as I said here the products cash key it's just an identifier and then we maybe get out our products as an eye numberable now if we do as I said we put them out so they are available in the scope of the index method if we have them in the in the key because we're using if we have them in the cache because we can use the out here now we also lock something products found in cash right awesome and if we don't have them in the cache we can see products not found in cash so we simply take our database context we take all of our products and we put them in a list make sure and this is very crucial to not store the query in the cache because in that case you will get an exception because it says like okay your instance of your database context is not well working anymore right dependency injection is working differently so you have to make sure that you don't store the query you have to store well a collection so the list the array or whatever that's why we say to list now if you have no clue about the difference between an i queryable and an I enumerable check out our video on that topic since that's also very important for all asp.net core developers well let me say that we really offer so much value for any.net developer out there so again if you haven't subscribed to our channel do it right now I'm absolutely sure that you will take amazing value from it now next up we will dig deeper into the cache options so we just create a memory cache entry options that's fine and now we got three things here to set up we got the set sliding expiration we set it to 45 seconds we've got the absolute expiration we set it to 10 minutes and we got the priority and we set it to normal now let's dig deeper into that so what is the sliding exploration well let's assume that for sure data changes in a database right now if we have an application that gets used by a lot of users you don't want to store the data for too long because well at any given time you want to update the data right so we've added new products and you for sure want to see those new products so this this is why we say like okay let's just save it 45 seconds something like that is really the industry standard something around 60 seconds right and after 45 seconds if no one in the meantime has requested the cash it will get cleared so that means that this is sliding if after 30 seconds another user requests the cash it will get set back to zero okay that's the sliding expiration now for the absolute expiration it does really care about the sliding expiration so anyway after 10 minutes the cache will get cleared so that new creative products will appear very important okay so the absolute exploration is entirely independent from this sliding expression so after 10 minutes it will get deleted anyway now the priority is let's just take a look here that's easier to explain we have low priority normal and high and never remove so if we select never remove right here the cache will never get removed right so we simply set the priority to cash item priority normal now afterwards you can see since we are here where products are not found we are grabbing the products we're configuring the cache we simply set the elements we save it into the memory cache so we use the key again we store the products we submit our configured options now here I stop the watch right so again here I started it here I stopped it so that we can really see the Delta time awesome and then we just lock the information return the view and see our data also I created a um an action results so a method that simply removes something from the cache you simply take that cache object called the remove method and submit the cache key so that you are able to for sure clear your cache that's also something that you want to do what you can do is if you add something new or delete something um like you add a new product or you delete a product maybe you are interested in just immediately clearing the cache so that it's always fresh keep that in mind so this is is basically what's going on here now I want to point out and that's very very important right there is a possibility on running into a race condition which basically means that if we have two users which are using the cache at the same time chances are that they populate it and we run into that and we have a race condition so what you'd have to do in that scenario is you have to create a lock to make sure that it's kind of thread safe right so if you're interested in seeing that please write it down into the comment section below I'd be happy to make another video on that topic now let's start our application one more time let's just take a look at the debug log here so it's starting and for sure a lot of time flies by right we can see everything here now it's loading the stuff the page gets loaded it says products not found in cash loading them right now blah blah blah all of that takes really a lot of time and on the other screen I can see that it's now showing up here you can see like 110 000 milliseconds but that's not the True Value it took to only load the products right so here we got our page now let me just clear the cache so I will simply click on clear cache so you can now see that the products got removed click cache and it took 99 milliseconds to load them from the database and put them into the cache now if I simply reload the page again stuff will now get loaded from the cache because 45 seconds our sliding expiration time has not passed yet so I'm loading it from the cache and other users would do that too you can see 23 milliseconds which is a nice saving let's just try that again how much time do we have now two milliseconds which is definitely awesome 50 times faster right if I just reload it again 31 milliseconds anyway it really depends on the well as I said right now my computer is working quite hard because of all of my screen recording programs and all of that but you can definitely see that it's so much faster right five milliseconds compared to 100 milliseconds or even more is definitely a nice Improvement well and now our products are quite simple ID name and price but there might be more complex objects right let's say you got some dependencies and foreign and all of that in that way it would take longer to load them from the database and caching becomes even more important so this is the basic introduction into memory caching in asp.net core I hope you liked that video make sure to subscribe to our Channel and check out our c-sharp progress Academy as I said you can find the link in the description below or in the very first comment and I'd be super happy to see you back in the next video thanks for watching foreign
Info
Channel: tutorialsEU - C#
Views: 13,091
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Step by step, development, programmer, learn how to, c#, .net, .net core, dotnet, core, asp, asp net, c sharp, csharp, memory caching asp, caching asp.net core, asp.net core web api caching, response cache asp.net core, cache in asp.net core, distributed caching, distributed cache, caching in asp.net, caching in asp.net mvc, cache in asp.net c#, visual studio, web api caching c#, dot net, how to code, caching, cache, in-memory distributed cache
Id: MSUTojuUEX4
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Thu Feb 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.