BrandGhost

Just ONE LINE of Code! FusionCache as Hybrid Cache in ASP NET Core

In this video series, we've been working with Entity Framework Core, Dapper, Redis, and different caching tools that we have access to in ASP NET Core. But we saw that Hybrid Cache and FusionCache have different APIs! Here's how we can configure FusionCache as a Hybrid Cache with just a single line of code.
View Transcript
All right. So, you started building your ASP.NET Core application and leveraging hybrid cache from Microsoft. And then you heard about Fusion Cache and how awesome it is, and then you found yourself going, "Oh no, I have to go rewrite all of my caching logic now because I want to switch over to it." Is that your only option, though? Hi, my name is Nick Cosantino and I'm a principal software engineering manager at Microsoft. In previous videos that I put together on caching using hybrid cache, fusion cache, and Reddus, we got to a point in those tutorials where I mentioned, hey, look, the hybrid cache API and the fusion cache API are different. So, we had to go make some changes to what those classes look like. Now, I mentioned in those videos, which you can watch up here in this playlist if you haven't already, that I would make this video explaining, that it's actually very easy to switch over to have Fusion Cache be compatible with hybrid cache. If that sounds interesting, just a reminder to subscribe to the channel and check out that pin comment for my courses on dome train. Let's jump over to the fusion cache GitHub and see what's up. All right, so we do have hybrid cache available for us inside of ASP.NET Core thanks to Microsoft and we have fusion cache which is a Nougat package and it has iFusion cache interface and you might have built your code your ASP.NET a core web app using hybrid cache or fusion cache, one or the other, but they're not compatible on the surface. Jodi Denetti, who is the maintainer and author of fusion cache, did add support for fusion cache as the implementation of hybrid cache. And I just wanted to go through his documentation, which by the way, every time I have to shout him out because his documentation is absolutely awesome. But let's go through this before we jump into the code to look at what we can do. So the TLDDR right at the top is fusion cache can also be used as an implementation of the new hybrid cache which he mentions is an abstraction for Microsoft right that's a key point here hybrid cache is an abstraction then he says with the added extra features of fusion cache he has a list which we'll see at the bottom of this document which explains some of the differences between these two packages but we can get the benefits of fusion cache even though we're using the hybrid cache interface and then he mentions oh and it's the first productionready implementation of hybrid cache. And it's kind of funny because right here we can see that fusion cache became the first productionready implementation of hybrid cache at all even before Microsoft had an implementation of hybrid cache. So kind of awesome to see. He explains some of the details up here but then he goes on to say like he mentioned in the intro hybrid cache is just an abstraction. It doesn't actually do anything just out of the box. It's an abstraction that we have implementations for. This is literally how easy it is to make fusion cache operate as the hybrid cache abstraction. Boom. Right here as hybrid cache. Literally this one extra method call. And I will prove it to you when we go back to Visual Studio and we can see how easy this is. He has some setup code here. But what I'm more interested in down at the bottom is that we have code in our application that looks very much like this where we're adding fusion cache. We have a serializer. We hook up Reddus. So we'll see this in Visual Studio in a moment. And then we actually have as hybrid cache that we'll add at the end. And we can leave the rest of the dependency injection the exact same as it was. Scrolling back up. I just wanted to call this out. I do recommend checking out Jod's documentation here. So he has a feature comparison that you can go look through. And this is going to be helpful because you might not even need to switch to Fusion Cache if you don't need any of these features. This might just be an interesting video for you to see what's going on. on it that you can use it. However, if there are fusion cache features that you want to go take advantage of and you already started coding against this hybrid cache abstraction, this will be a helpful video for you. So, scrolling down a little bit more, he has a little bit more details here, right? So, you can go read about some of the extras and then even more. So, he writes a ton in his documentation. Like I said, I highly recommend checking it out. even things like keyed services and things like that and calls out limitations as well. I do want to go switch over to the code and see this up and running. So this is Visual Studio with our previous project that we had going on in the previous two videos and I mentioned the playlist at the beginning of this video. The last two we looked at setting up Entity Framework Core with Reddus and Fusion Cache and then we looked at Dapper with Fusion Cache and Reddus as well. So we could flip between those two implementations. But there was a problem. If I jump into this dependency injection code, if we look at these two methods here, the original code using hybrid cache, we had this decorator class called a hybrid caching repository. And we had to go switch to this fusion cache repository. And that's because these two repositories had to use either a hybrid cache or fusion cache. Right? So if I jump into this, you can see that this one in the constructor takes in a hybrid cache on line 15 and the other implementation unfortunately had to be made because it takes in I fusion cache. Right? So if I go jump into this one now just to show you, you can see that it takes in I fusion cache instead. So, when I made the other video, I said, "By the way, I'll make this current one that you're watching right now to explain that we don't have to go do all of this stuff, right? It only needed to be done if we're not using this little secret." And this is how easy this little secret is. Ready? I'm just going to say as hybrid cache and we're done. That's literally all that has to happen. But you can see up here I'm still using the fusion cache decorator. I'm going to go put this back to the hybrid cache one. We'll start with entity framework core. The two things that I've done, I've switched back to entity framework core with this one line. I've switched to hybrid cache as well, which you can see right here and I'm using as hybrid cache. The entire rest of this project has been unchanged. I didn't go touch the repository. I did not go touch the uh web API methods here. Everything is left the same except how we've configured this. One of the reasons I made this video series and you're watching a later video in the series is that I wanted to show that we can mix and match some of these pieces with a couple of design patterns. And that way we could compare and contrast things like hybrid cache, fusion cache, dapper, entity framework core, all this kind of stuff just by having modular pieces that we can tune with these couple of changes. What I'm going to do is run this application. Just to mention it, I already have Docker running. Docker is running. This is a Reddus instance and I wanted to make this note because we have Reddus hooked up and what will happen is when we make our first web request, this Reddus instance already has that information loaded up. We already have a value for the key that we're going to be accessing. It's not in the memory cache yet, but it will be once we go to run this application because it will get it from Reddus. If it was not, when I go to run this application, we would see entity framework print out the SQL statement that it's using to fetch it from the database. So, let's go ahead and run this. Okay, so the ASP.core web applications up and running. I have a new tab pulled up here. Let's get this in a spot where you can see what's going on. I'm just going to press enter on this uh path. We can see that this ID is one that I have here. This is going to call our get method. And you can see that fusion cache printed out some stuff because we have a cache hit. But we didn't see entity framework core print anything. Entity framework core is configured. It did not print out the SQL statement to fetch from the database because it was able to go to Reddus to get this information. And if I keep pressing enter, we will not see any SQL hit. Pretty simple example. I'm going to stop that. And just to show you how easy this is now, I'm just going to say Dapper with hybrid cache. This will still work. It's going to use the fusion cache, but it's as hybrid cache. And if I go into this method, you can see that it's using the hybrid caching repository. I can literally comment this one out. It's not going to be used. We're using the hybrid caching repository, which is our decorator around another repository. We have that. If I go run this now, let's go press play. we should see the same exact behavior. The difference is that when we're using Dapper, it's not going to print out the SQL statements when we have to go through the repository. Let me stop this and actually show you again. I'm going to put a breakpoint in the constructor for the repository for Dapper. And then I'm going to put a breakpoint in the get method. Because it's not printing out to the console, I'm just going to use a breakpoint to demonstrate. Okay, so the application's running. I'm going to go back to the browser and when I press enter here, the order of operations we should see are that we're going to hit a break point in the constructor of the Dapper repository because it's saying, hey, I need this dependency passed in. But then what's going to happen is that we should be able to get this from Reddus thanks to fusion cache. So let's press enter. There's the first breakpoint that we hit. So, as I mentioned, we're not going to hit this breakpoint, though, because the value is going to come from Reddus instead. Press F5. We got the value. And if I go back to the console, you can see the fusion cache debugging logs here. This is just a super quick video because if you've watched the previous videos in this series, I was saying that we had to go build this extra Fusion Cache repository. And that's because fusion cache does not implement hybrid cache. It is not the same interface, but the author of fusion cache has added in this little method for us as hybrid cache, which enables us to be able to leverage it the exact same way that we would hybrid cache. I hope you found that helpful. And like I've said a few times throughout this video, if you've not watched the previous videos inside of this series, I would highly recommend starting from the beginning or going through them and picking a familiar spot for yourself. And that way you'll understand more about how the repositories were set up, how the decorator pattern works, and then what some of these methods do on our web API. So, thanks so much for watching and I'll see you next time.

Frequently Asked Questions

What is Fusion Cache and how does it relate to Hybrid Cache in ASP.NET Core?

Fusion Cache is a NuGet package that provides an implementation of caching for ASP.NET Core applications. It offers additional features compared to the Hybrid Cache, which is an abstraction provided by Microsoft. The cool part is that Fusion Cache can be used as an implementation of the Hybrid Cache interface, allowing developers to leverage its benefits without needing to rewrite all their caching logic.

Do I need to rewrite my existing caching logic to switch to Fusion Cache?

No, you don't have to rewrite your existing caching logic. By using a simple method call, 'as hybrid cache', you can make Fusion Cache compatible with your existing Hybrid Cache setup. This means you can continue using your current code structure while taking advantage of the features offered by Fusion Cache.

Where can I find more information about using Fusion Cache and Hybrid Cache?

I highly recommend checking out the documentation provided by Jodi Denetti, the maintainer of Fusion Cache. His documentation is extensive and covers feature comparisons, setup instructions, and limitations, which can help you understand how to effectively use both caching solutions in your ASP.NET Core applications.

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