Simple Caching with FusionCache in .NET
45sQuick, practical demo of implementing caching in .NET that developers can immediately apply, making it highly shareable.
▶ Play Clip"The title promises a simple example and delivers exactly that, though it's brief and lacks depth."
This video demonstrates a simple implementation of caching using the Fusion Cache library in a .NET application. It shows how to use the iFusionCache interface to implement the cache-aside pattern, improving performance by storing frequently accessed data.
The video starts by explaining a simple example of implementing caching using the FusionCache library, focusing on generating a cache key and a variable for a cacheable DTO.
The iFusionCache interface exposes a GetOrSetAsync method that allows easy implementation of the cache-aside pattern. The method takes a cache key and a delegate acting as a factory to fetch the value if not present in the cache, such as a typical EF Core query.
Caching options can be configured, including cache duration and an optional failsafe. If a null value is returned, the code returns Not Found; otherwise, it returns the value from the controller endpoint.
If the value wasn't present in the cache on the first request, it will be there for subsequent requests, improving performance.
The video provides a straightforward demonstration of using FusionCache to implement caching in a .NET application, highlighting the simplicity and performance benefits of the cache-aside pattern.
What method does iFusionCache expose for implementing the cache-aside pattern?
GetOrSetAsync
00:15
What is the purpose of the delegate in GetOrSetAsync?
It acts as a factory to fetch the value if it's not present in the cache.
00:15
What happens if a null value is returned from the cache?
The code returns not found.
00:27
What are two caching options that can be configured?
Cache duration and an optional failsafe.
00:27
Cache-aside pattern with GetOrSetAsync
Shows a clean, idiomatic way to implement caching in .NET, reducing boilerplate code.
00:15Performance improvement
Highlights the direct benefit of caching: faster subsequent requests.
00:41[00:00] Here's a simple example of how you can implement caching using the Fusion Cache library. I'll start by generating a cache key and a variable to hold my cacheable DTO. The iFusionCache interface exposes a get or set async method,
[00:15] which allows us to easily implement the cache aside pattern. I'll specify the cache key and a delegate that's going to act as a factory to fetch the value if it's not present in the cache. This could be your typical EF core query.
[00:27] you can also configure the caching options such as the cache duration and an optional failsafe if you do end up getting an all value then we can just return not found otherwise we're going to return this value from the controller endpoint and if it wasn't present in the cache on the first
[00:41] request it's going to be there for any subsequent requests which is going to improve our performance
⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.