TubeSum

FusionCache for .NET — Step-by-Step Guide & Transcript

The Only .NET Caching Library You'll Ever Need?

0h 14m video Published Mar 20, 2026 Transcribed Aug 8, 2026 M Milan Jovanović
Intermediate 7 min read For: .NET developers familiar with dependency injection and basic caching concepts.
AI Trust Score 65/100
⚠️ Average / Some Fluff

"Delivers a solid overview of FusionCache, but the title overpromises—it's a good library, not the only one you'll ever need."

AI Summary

This video provides a comprehensive introduction to FusionCache, an open-source caching library for .NET applications. It demonstrates how to implement caching using the cache-aside pattern, configure two-level caching with Redis, and use the backplane for distributed environments. The video also covers cache invalidation and the hybrid cache abstraction.

[00:01]
Introduction to FusionCache

FusionCache is an open-source, easy-to-use caching library for .NET, built by Julie Lerman. It offers two-level caching (in-memory and distributed), a backplane for synchronization, and extensive documentation.

[01:20]
Installing FusionCache

Install the Ziggy Creatures FusionCache library in the application layer to access core abstractions and dependency injection support.

[02:27]
Cache-Aside Pattern with GetOrSetAsync

Use the GetOrSetAsync method with a cache key and a factory function to implement the cache-aside pattern. The factory contains the database call, and the result is cached for subsequent requests.

[03:39]
Dependency Injection Setup

Register FusionCache in the infrastructure layer using the AddFusionCache extension method on IServiceCollection. The default implementation uses in-memory caching.

[04:31]
Configuring Cache Options

Configure cache duration via WithOptions and DefaultEntryOptions. Set default cache duration to 5 minutes for both in-memory and distributed caches.

[07:27]
Cache Invalidation

Invalidate cache entries by calling RemoveAsync with the same cache key in command handlers (e.g., delete or update operations). Use a helper class for consistent cache key construction.

[09:35]
Distributed Caching with Redis

Add Redis to Docker Compose, install serialization and distributed cache libraries, and configure FusionCache with a serializer and RedisCache. This enables two-level caching without changing application code.

[13:14]
Hybrid Cache Support

FusionCache is the only production-ready implementation of the hybrid cache abstraction. Replace IFusionCache with HybridCache to avoid non-Microsoft abstractions, with minor method name changes.

FusionCache simplifies caching in .NET applications with its two-level caching, backplane, and hybrid cache support. It is a production-ready solution that can significantly improve performance and scalability.

Mentioned in this Video

Tutorial Checklist

1 01:20 Install the Ziggy Creatures FusionCache library in your application layer.
2 02:27 Use GetOrSetAsync with a cache key and factory function to implement cache-aside pattern.
3 03:39 Register FusionCache in the infrastructure layer using AddFusionCache extension method.
4 04:31 Configure cache options like default duration using WithOptions and DefaultEntryOptions.
5 07:27 Implement cache invalidation by calling RemoveAsync with the same cache key in command handlers.
6 09:35 Add Redis to Docker Compose and install serialization and distributed cache libraries.
7 10:41 Configure FusionCache with a serializer and RedisCache for two-level caching.
8 13:14 Optionally replace IFusionCache with HybridCache to use the hybrid cache abstraction.

Study Flashcards (9)

What is FusionCache?

easy Click to reveal answer

An open-source, easy-to-use caching library for .NET applications.

00:16

What are the two levels of caching in FusionCache?

easy Click to reveal answer

In-memory and distributed cache.

00:44

What method is used to implement the cache-aside pattern in FusionCache?

medium Click to reveal answer

GetOrSetAsync.

02:40

How do you register FusionCache in dependency injection?

medium Click to reveal answer

Call services.AddFusionCache() on IServiceCollection.

04:05

What is the default caching provider in FusionCache?

easy Click to reveal answer

In-memory provider.

04:19

How do you invalidate a cache entry in FusionCache?

medium Click to reveal answer

Call RemoveAsync with the same cache key.

07:27

What is the purpose of the backplane in FusionCache?

medium Click to reveal answer

To synchronize the distributed cache between multiple application servers.

00:44

What is the hybrid cache abstraction in FusionCache?

hard Click to reveal answer

A way to replace IFusionCache with HybridCache to avoid non-Microsoft abstractions.

13:14

Which Redis library is used for the distributed cache in the video?

medium Click to reveal answer

Microsoft.Extensions.Caching.StackExchange.Redis.

10:29

💡 Key Takeaways

📊

FusionCache is open-source and easy to use

Establishes the library's credibility and accessibility for developers.

00:16
🔧

Cache-aside pattern with GetOrSetAsync

Shows a practical, common pattern for implementing caching in real applications.

02:27
🔧

Cache invalidation is straightforward

Demonstrates how to keep cached data consistent with database changes.

07:27
📊

FusionCache is the only production-ready hybrid cache implementation

Highlights a unique selling point that addresses distributed caching challenges.

13:14

[00:01] .NET applications, there is one library that you should know about and it's called FusionCache. In this video, I'm going to give you a general introduction into the FusionCache ecosystem and what it can do for your .NET applications.

[00:16] First of all, what is FusionCache? It's an open-source and easy-to-use caching library and it's built by Julie Lerman, who I've had a chance to meet at the Microsoft MVP Summit last year and I can tell you he's an awesome guy building a

[00:29] stand out is first of all the functionality and the performance that it offers, but also the extensiveness of the documentation. I mean, just look at the diagrams here, absolutely beautiful. It comes with two-level caching with an

[00:44] in-memory and a distributed cache. It also has a backplane for synchronizing the cache between multiple application servers and the documentation is just great. You get everything you need to get started. There's docs on the

[00:56] dependency injection support. There's docs on the level one and level two caching. There's documentation on the backplane and how it works to synchronize your distributed cache between multiple services and there's

[01:08] docs on the hybrid cache support, which I'm going to talk in the end. And a fun piece of information is that FusionCache is currently the only production-ready implementation of the hybrid cache abstraction. So, enough about the intro.

[01:20] FusionCache to our applications. I'll start by installing the core library inside of my application layer and I'm going to look for FusionCache. And I know somebody's going to ask this in the comments. No, this video is not

[01:34] making it for the love of the game. So, what I'm going to install inside of my application layer is the Ziggy Creatures FusionCache library. This will give me access to the core abstraction and I can also wire it up with dependency

[01:49] injection. Now, let me open up some use case like the get to do by ID query handler and this is a good example of where you might want to introduce caching. So, we're fetching an entity from our database based on its

[02:02] identifier and we can expect that this is a fairly frequent operation. So, we want to introduce caching to improve performance. When we cache the result of this database call, we're going to have it in memory where we can continue

[02:14] serving it for a given period of time. We can control this in our setup and also for individual cache operations. And this lets us satisfy this query much faster than if we were to reach out to a database to query for this to do item.

[02:27] So, the core abstraction, as you can see, is the I FusionCache interface and a good pattern to use inside of our query handler is the cache-aside pattern. Now, obviously, FusionCache supports this and the method that you

[02:40] want to use is called get or set async. Now, it accepts a cache key and I could use something like to do and then pass in an interpolated value for the to do item ID, which I can grab from my query. And then, I can define my factory

[02:55] function that gives me access to the cancellation token. Let's also make it async. And what we want to move here is our database call, which fetches the to do item. So, then I can return this. So, we want to return our to do. We also

[03:11] want to pass in the cancellation token as the argument and we want to use the token provided in our factory function. Let's go ahead and await this and we're going to be using this to do item here, which is the response from the get or

[03:25] set async method. So, you can see how easy this was to use and in a minute or two, we've already caching implemented using the FusionCache abstraction. Now, we are injecting this as a service, but how are we going to wire this up with

[03:39] dependency injection? I'm going to do that from the infrastructure layer because I also want to add some external abstractions later. So, I'm going to keep this scope to infrastructure. Now, let's call this the add cache method,

[03:52] which currently doesn't exist, but I'm going to define it as an extension method here on the I ServiceCollection interface. So, we're calling it add cache. It's an extension on the I ServiceCollection and what do you need

[04:05] to define FusionCache? You're going to say services add FusionCache and that's it. This is the most basic setup and now FusionCache will be available as a service to inject within the dependency injection provider and we can safely

[04:19] inject it into our use cases. The default implementation is going to use the in-memory provider for caching and this might be sufficient for most applications out there. How about configuring FusionCache? For example, we

[04:31] may want to set things like the cache time. So, you can say with options, import the missing using statement and then you have access to the FusionCache options. And this lets you configure so many things that we probably can't cover

[04:45] in this video. Now, I want to show you the default entry options. For example, you want to configure what is the default duration for your cache entries? Let's say we want to cache them for 5 minutes. And you can also configure the

[04:57] distributed cache duration and let's say it's also 5 minutes. There are other extension methods. For example, you can configure the default entry options directly without having to go through the top-level options and this just

[05:10] simplifies your setup to directly access the default entry options. So, either of these is fine. I'm just showing you a couple of examples. So, let's go back to our use case. I want to place a breakpoint here and a breakpoint in our

[05:23] my application. And this will start my application inside of Docker Compose with my API and database running and I've also got a Seq instance for logging. Now, if I navigate to localhost:5001/swagger,

[05:37] I can see my Swagger UI where I can go ahead and quickly create a test user. So, let's say [email protected]. Let's set some password. I'll execute this and you can see this completes. We get a user ID back. I can then use this

[05:52] in the login endpoint minus the first name and last name to get back an access token. So, this is what we're going to use to authenticate with our API. So, I'll just say authorize and that's done. And I am going to grab the user ID from

[06:07] the response that we got here and then let's go ahead and create a to do item. We're obviously going to need this in order to test out our caching support. So, this completes. We get back a to do item ID and finally, I can go ahead and

[06:20] send a get request to fetch this from the database. And this is where we hit our initial breakpoint. Now, because we don't have this cached yet, we're going to execute our factory function and you can see we hit our second breakpoint.

[06:33] So, press continue and we get a response back in the Swagger UI. So, to test this out, I'll send another request and we immediately hit our first breakpoint, but if I hit continue, you will see that we won't land on the second breakpoint.

[06:46] FusionCache is going to return the cached value instead of us having to reach out to the database and this will be much faster. If I jump into the Seq UI for a moment, we can see some logs from FusionCache here and this is our

[07:00] initial request where we encounter a cache miss followed by our database query and then a log from FusionCache storing the value in the in-memory cache. And then, here's our subsequent request where you can see that we are

[07:12] without having to reach out to the database. What about cache invalidation? Let's say I want to invalidate this cache entry whenever I delete this to do item so that we don't serve any cached value. Well, you can do that very

[07:27] easily. I'll go into the delete to do command handler. You just inject the FusionCache abstraction and after I call save changes here, I'll say cache remove same cache key. So, this is going to be to do's and from the command, I can pull

[07:43] in the to do item ID, pass in the cancellation token and we've got cache invalidation. Let's say I also want to do this inside of the update to do command handler. Again, the same game plan. Just inject FusionCache and I can

[07:56] go ahead and reuse the call from my previous implementation and call remove invalidation. Now, the problem I have here is having to repeat the cache key in this format, which isn't very refactor-friendly. So, what you can do

[08:11] is define a simple helper class. I usually call it cache keys and it looks something like this. It's a static class with other constants or helper methods inside. So, I've got a cache key for a to do item by ID where you pass in the

[08:25] to do item ID and you get back a cache key value. How do we use this? Well, I can now replace this with a function call. So, I'll say cache keys and then call. So, I'll say cache keys and then to do by ID and I just pass in the to do

[08:38] item ID. I can do the same in my command handlers. So, now I've got a consistent way to construct my cache keys and whenever I want to, I can refactor this and it won't break any of my caching code. If I want to, I can even move this

[08:52] into a domain event handler. So, let's say I introduce a new class called to do item deleted domain event handler because I already have support for this within my code base, I can implement an I DomainEventHandler

[09:06] I DomainEventHandler interface for the to do item deleted domain event. Let me go ahead and add that. Let's add the implementation and I'm going to need my I FusionCache abstraction and I can just move the code

[09:19] that I've got here into my domain event handler, make sure that this is async this is also going to function the same. What about distributed caching? Well, we can also do that inside of FusionCache. So, we're going to need a distributed

[09:35] cache to start. The most popular choice is probably Redis and I'm going to drop in my configuration. I'll use an Alpine image to reduce the image size. We're going to expose it on the default port and we need to define a connection

[09:47] string inside of our appsettings.json to be able to connect to this. Now, the are running in our Docker network, we can define the service name and the port application will be able to connect to it. Now, we will need a few more

[10:01] libraries to be able to support this and these I'm going to install in the infrastructure project. So, if I look for FusionCache again, there's this serialization library that I want to install and you want to have this when

[10:14] you've got a two-level cache like in our example we are going to introduce Redis. although we don't need it just to show you what the setup looks like and we're going to use StackExchange.Redis and we want to look for a distributed cache

[10:29] to install Microsoft.Extensions.Caching.StackExchange.Redis. need and we have to configure this with FusionCache. So, I'm going to need

[10:41] access to the connection string. So, what I will do is pass in IConfiguration here, update this to also expose the IConfiguration instance and I'm going to grab the connection string which I believe I called Redis. Let me double

[10:55] check that. So, yes, that is correct. So, now we've got our connection string and the libraries that we need. So, let's start with the serializer setup. You just say with serializer, you pass in the new FusionCache.System.Text.Json

[11:08] Serializer and if you ever want to swap this out for example with Newtonsoft.Json, you can just install the respective library and use that as cache. To configure our distributed cache, we just say with distributed

[11:23] cache. Now, we have to provide in an implementation of the IDistributedCache interface and the simplest option is newing up a new RedisCache instance and here I'll say new RedisCacheOptions and we want to provide the configuration

[11:37] What about the backplane? It's very similar. You say with backplane new RedisBackplane. This is available in the library that we just installed and the same idea. We just pass in the connection string and we can connect to

[11:52] our backplane. And now with no changes to our application code, we have implemented two-level caching backed by FusionCache. To show you that this works, let me start the application and back in our Swagger UI, I'm going to

[12:04] send a couple of requests with different to do item IDs to just force them to be cached. Now, the first request here is going to execute this and give us back the result but it's also going to cache it in both the first-level cache which

[12:19] is in memory and the second-level cache which is inside of Redis. If we take a look at my Docker Compose setup, you can see that Redis is up and running and if inside of Redis, we can you can see we have three cache keys inside and this is

[12:33] the data that FusionCache stores for your cache entries. So, you've got the data property containing your serialized object and you've also got when this expires. So, if we for example invalidate this by causing a delete, let

[12:46] me grab the to do item ID. We can use the to do item ID from this request here, issue a delete request and I just placed a breakpoint in the domain event going to trigger it. We're going to call RemoveAsync. This will complete our

[13:00] cache invalidation and if I go ahead and refresh this, you can see that the value is now gone from our Redis instance and it's also removed from our in-memory cache. And for the end, a cherry on the top, I mentioned at the start of the

[13:14] video that FusionCache is the only production-ready implementation of the because the default Microsoft abstraction doesn't have a backplane and it's going to run into problems when working in a distributed environment.

[13:27] The FusionCache backplane solves this. However, it also comes with a hybrid cache implementation. So, what this allows you to do is to replace the IFusionCache interface with hybrid cache if you don't want to depend on

[13:41] non-Microsoft abstractions. So, we can use hybrid cache here and just replace the abstraction and just replace FusionCache with hybrid cache and the see what the cache invalidation looks like. Let's do the same inside of the

[13:56] update handler. I just have to remove the argument name and the RemoveAsync compiles. The GetByID is slightly different. If I replace FusionCache with hybrid cache, the method is called GetOrCreateAsync but otherwise it's

[14:09] going to look more or less the same with maybe a different method name here and there and some parameters mismatch but I think this is a very compelling use case for the FusionCache library and we've only just scratched the surface here. I

[14:21] more advanced options that you can ex- plore which I'm going to leave for some other video in the future. If you want to see how you could solve the hybrid cache problem and implement cache

[14:33] environment, then I highly recommend taking a look at this video next. If you enjoyed this video, smash that like button so it gets recommended to more .NET developers. Thanks a lot for watching and until next time, stay

[14:48] watching and until next time, stay awesome.

More from Milan Jovanović

View all

⚡ Saved you 0h 14m reading this? Transcribe any YouTube video for free — no signup needed.