Working with FusionCache in ASP.NET Core: A Step-By-Step Guide

SeniorTechInfo
3 Min Read

The Power of FusionCache in Your ASP.NET Core Application

When it comes to improving performance and responsiveness in your ASP.NET Core application, caching is key. However, implementing caching can sometimes be tricky. This is where FusionCache comes in. FusionCache is a powerful caching library that simplifies the process of implementing and managing caching in your application.

In this article, we will explore how you can leverage FusionCache in your ASP.NET Core application to enhance performance and maintain up-to-date data in your cache.

Implementing FusionCache in Your ASP.NET Core Application

With FusionCache, implementing caching in your ASP.NET Core application is seamless. Let’s take a look at a sample code snippet that demonstrates how FusionCache can be integrated into a controller in your application:


using Microsoft.AspNetCore.Mvc;
using ZiggyCreatures.Caching.Fusion;

namespace FusionCacheExample.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly IProductRepository _productRepository;
        private readonly IFusionCache _fusionCache;

        public ProductController(IFusionCache fusionCache, IProductRepository productRepository)
        {
            _fusionCache = fusionCache;
            _productRepository = productRepository;
        }

        [HttpGet("{productId}")]
        public async Task GetProductById(int productId)
        {
            var cacheKey = $"product_{productId}";
            var cachedProduct = await _fusionCache.GetOrSetAsync(cacheKey, async () =>
            {
                return await _productRepository.GetProductById(productId);
            },
            options => options
                .SetDuration(TimeSpan.FromMinutes(2))
                .SetFailSafe(true)
            );

            if (cachedProduct == null)
            {
                return NotFound();
            }

            return Ok(cachedProduct);
        }
    }
}

Support for Eager Refresh in FusionCache

FusionCache includes a great feature called eager refresh that can help you keep your cache updated with the latest data while ensuring responsiveness at the same time. By enabling this feature, you can specify a custom duration for your cached data and a percentage threshold. Check out the code snippet below:


options => options.SetDuration(TimeSpan.FromMinutes(1))
options => options.SetEagerRefresh(0.5f)

With eager refresh enabled, FusionCache will automatically refresh the cache in the background when the cached data is older than 50% of the cache duration. This ensures that your cache remains up-to-date without sacrificing responsiveness.

By integrating FusionCache into your ASP.NET Core application and leveraging features like eager refresh, you can significantly improve performance and ensure that your application remains responsive even under heavy load. Try FusionCache today and experience the difference it can make in your application!

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *