BrandGhost

Fusion Cache in C# - Removal, Expiration, and FailSafe Cache Operations

Fusion Cache is an awesome third-party package that we can use in DotNet for both in-memory and distributed caches. Let's dive into how the removal and expiration of cache entries work alongside the fail-safe mechanism!
View Transcript
hi I'm Nick centino and I'm a principal software engineering manager of Microsoft in this video we're going to be looking at Fusion cache and in particular the fail safe feature with respect to expiring records in the cash now if you're not familiar with Fusion cach it is an alternative third-party Nate package that we can leverage for a hybrid cache solution that means that it does support in memory as well as distributed caching mechanisms we'll look specifically at the fail safe mechanism expiring and removing cash entry so if that sounds interesting just a reminder subscribe to the channel and check out that pin comment for my courses on dome train now let's jump over to visual studio and see everyone's favorite sample application the weather forecast app all right to get started with Fusion cache we need to make sure that we're adding the NIT package so after we have the N get package added we need to make sure that we have the cash available through dependency injection so I'm just adding Fusion cache right here at the top on line four and then from there we're able to access it through dependency injection on our minimal API so line 10 here you can see that have iuse cach and then I have the cash reference coming in that we can work with now in this example it is just going to be the weather forecast app if you've made an asp.net core application and visual studio before this is the very basic thing that they put together for you out of the box what we're going to be doing is that we have this Factory method right here and that means that when our entry is not found in the cache based on this key that we have on line 13 I'm just calling it cach key this would be a little bit more complicated or custom tailored in your application but in this one it's just going to be called Cash key if it's not found we'll call the factory method we'll go look at that in just a moment but the other thing to note is that I'm going to be setting the is fail safe enabled property here on the fusion cash entry options what exactly is fail safe here well if I jump over to the documentation website for Fusion cache they have a very good explanation and in fact a lot of their documentation is really good with examples and pictures so I think if you need a a little bit more information or want to dive deeper on this kind of stuff their documentation is great and like I said even for the other features in Fusion cache you should able to check out their docks and I think they do an awesome job in a nutshell the failsafe method allows us to be able to have a fallback cash entry if we were to expire something and that's because in more complex distributed systems if we were to expire a cash entry or it has expired naturally based on the time setting that we have if something is going to go fetch the new value so calling our Factory method if that Downstream service ends up throwing an exception for some reason and we originally had something there in the cach that had just expired we do have this opportunity to say okay even though we failed fetching the new value let's just return to our caller the last one that was there so we are saying that we're going to give our caller a stale value this might not be okay in every scenario which is why it's an option so this is a fallback opportunity that if you do have a problem that we can go back to some stale value and that way at least the service is not disrupted or your API call is not disrupted and then someone can continue even though the value is stale now before we go back to the factory method implementation here I'm just going to show you two more API calls that I've added and that's going to be an expire route and a remove route and that means that we have two different options here with Fusion cash to be able to remove entries the first one is going to be expire and the second one is remove as names on the API calls sort of indicate right so the expire one if we check out the documentation it says expires the cash entry for the specific key that can mean an expire if the fail safe was enabled when saving the entry or remove if fail safe was not enabled when saving the entry all automatically if we look back up at line 17 I do have fail safe enabled so that will mean that we are going to try and expire the entry so what does it mean to expire versus remove well let's go check out remove and it just says removes the value in the cach for the specified key right so this basically will directly remove it whereas expire has this option to keep the entry in the cach but say hey look this thing's stale and that means again when we have fail safe enabled if on a subsequent call to go do the factory method call again we're going to see this in just a second if we go back into here to try and get a new Fresh Value if it throws an exception we we can fall back to the stale one this is just a brief Interruption to remind you that I do have courses available on dome train focused on C so whether you're interested in getting started in C looking for a little bit more of an intermediate course focus on object-oriented programming and some async programming or are you just looking to update your refactoring skills and see some examples that we can walk through together you can go ahead and check them out by visiting the links in the description and the comment below thanks and back to the video let's go check out create forecast async which is our Factory method this is going to be a little bit contrived but it's just going to be to illustrate the example I have a parameter passed in called counter this isn't really a good real example but what we're going to do is throw an exception when our counter hits two so if I scroll back up you'll see that on line 14 I'm increasing this counter this is just going to be to simulate this example for us what we're going to do though is we're going to wait a full second when we're calling this that way we can kind of simulate that this API is taking a moment to respond when it's not getting the cached value and we can also see that we will write to the console and that way we have some type of visual indication when are Factory methods being called what we're going to try and do is run this application we're going to hit the API see that we have cached values and we'll see when the factory method's actually called then we'll play around with expiring and removing entries to see the behavior all right to kick things off I have the weather forecast app running I'm going to go ahead and go to weather forecast you'll see that it takes a moment and then we get results back if I pop this console open you can see forecast created zero so the first time we went in our counter is at zero if I go to call weather forecast again it finishes basically immediately so if I press enter again and again and again you can see that instantly we're getting results back and it's the same results that's because they're cached and to go back to the console you can see that for has created has not printed again we've only gone into that factory method the one single time so let's go ahead and expire this entry when I hit this route sorry I should have said flashbang warning but we expire it that means that the entry is no longer in the cash I wasn't returning any value though so let's go ahead and go back to weather forecast right we go do this now we see the weather forecast returned and we can see that forecast created one is the return value here that's going to mean that we ended up going back into the factory method yet another time just a heads up if it looked like this one completed very fast I've noticed that sometimes there's some pre-flight calls from the browser and we might have actually seen that when I was going into my menu to go autocomplete the forecast route that we needed to hit it might have already done the pre-flight and that way it actually happened very fast even though we still had to go into this it's just that it happened behind the scenes now let's let's go expire it one more time and remember the second time that we have to go back into the factory method it should throw an exception so let's expire it flashbang warning there we go expired now we're going to go back to the route and you can see just to prove it my auto complete kicked in and it actually did a pre-flight here I never pressed enter but you can see that the factory method we entered it again and we're throwing this exception it says oh no okay so I'm going to press F5 again and there we go so I never actually pressed enter yet like I said it was pre-flighting so I'm going to press enter now there we go it took a moment to go regenerate the cache entry but you can see that what ended up happening here was that we did this part here where it was the fail save and it was actually returning the value for us and then we saw this exception get l loed but you can see that right after we did forecast created 3 so unfortunately when I was demonstrating this example because of the preflighting that meant that we didn't get to see the cached value come back I decided that I would repeat this example but instead use two tabs here so that I don't have to use the drop down so I got us back to the state if I go back to the console you can see I've done forecast created the first time the second time I've just expired the cash entry so again if I go to press weather forecast enter right now we should be able to see this Behavior where we throw an exception and we're going to get the fail save value so just to make sure that we're looking at the same data temperature c23 summary says warm and then temperature F 73 so press enter there is the exception and we get 23 warm 73 right just looking at this first entry that we had here so this actually did give us the cached value you can see fail safe activated from memory so unfortunately like I said that first example because of the browser Behavior it ended up going in calling the API one more time so I couldn't show you the cach value made it a little bit messy but that's just how it worked out the second time through I made sure I used two tabs so we wouldn't have this Behavior just makes it for an easier example to follow along with but as you can see forecast created zero this is the second time we went in and then the third time which is when the count is actually two we throw the exception right then that means that we fall back to getting the fail safe value because we've enabled it now if I go to call this again you can see that we get another one and that's because the cash value was stale when we returned it right and we did the fail safe so going back to the console you can see that we now on the next attempt we said okay it was a fail safe attempt before right so we fell back now that someone's calling us again we're saying this value is stale it's been expired so we are going to try it one more time because I had that counter set to check for exactly two we've now passed two we've gone to three so it's not going to throw an exception again it's just a one-time thing and we were successfully able to go past that if statement and get the value back so now that we've seen how we can do fail safe with expiration I just wanted to show you explicitly doing removing so let's go ahead and run this we'll go back to the browser now if I call weather forecast we should go to the API once and do the factory method call right so if I go back over to the console we see forecast created what I'm going to do is instead of expire I'm just going to go to remove again it's going to be very bright I apologize so we remove it now if I go back to weather forecast there is the entry and we can see forecast created zero then forecast created one if I call this again again again it's instantaneous it's because it's cashed to quickly summarize the two options that we have if we want to remove a cash entry we can explicitly call remove and that means that the next time someone goes to get it it's not going to treat it as a potentially stale value that means it's just not going to be there it will mean that the caller because the cash entry is not there it will basically miss the cash go do whatever Factory method in this case it's just generating some random data and your system it maybe hitting a database or a downstream service and then it will cash that result after with the expire entry and the fail save combination it essentially allows us to keep a stale entry in the cache and that way if there's an error on the next subsequent call to do the factory method we can fall back to the stale value again this can be a helpful thing in your system if you need a little bit more resilience and you need to handle errors like this but again being able to return stale values may not be acceptable in your system so just more tools and options for you to work with thanks for watching and I'll see you next time

Frequently Asked Questions

What is Fusion Cache and how does it work in C#?

Fusion Cache is an alternative third-party NuGet package that I can leverage for a hybrid caching solution in C#. It supports both in-memory and distributed caching mechanisms, allowing me to efficiently manage cached data.

What is the fail-safe feature in Fusion Cache?

The fail-safe feature in Fusion Cache allows me to return a stale cached value if a new value cannot be fetched due to an exception. This ensures that my application can continue to function without disruption, even if the data has expired.

What is the difference between expiring and removing cache entries in Fusion Cache?

Expiring a cache entry keeps the stale value in the cache while marking it as expired, allowing for a fail-safe fallback. Removing a cache entry, on the other hand, completely deletes it from the cache, meaning the next request will not find it and will need to fetch a new value.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload