utilizing Microsoft.AspNetCore.Mvc;
utilizing ZiggyCreatures.Caching.Fusion;
namespace FusionCacheExample.Controllers
{
   [Route("api/[controller]")]
   [ApiController]
   public class ProductController : ControllerBase
   {
       non-public readonly IProductRepository _productRepository;
       non-public readonly IFusionCache _fusionCache;
       public ProductController(IFusionCache fusionCache,
       IProductRepository productRepository)
       {
           _fusionCache = fusionCache;
           _productRepository = productRepository;
       }
       [HttpGet("{productId}")]
       public async Process GetProductById(int productId)
       {
           var cacheKey = $"product_{productId}";
           var cachedProduct = await _fusionCache.GetOrSetAsync
           (cacheKey, async () =>
          {
               return await _productRepository.GetProductById(productId);
           },
           choices =>
               choices
                   .SetDuration(TimeSpan.FromMinutes(2))
                   .SetFailSafe(true)
           );
           if (cachedProduct == null)
           {
               return NotFound();
           }
           return Okay(cachedProduct);
       }
   }
}
Help for keen refresh in FusionCache
FusionCache features a nice characteristic known as keen refresh that may enable you to maintain your cache up to date with the newest information whereas guaranteeing responsiveness on the identical time. While you allow this characteristic, you’ll be able to specify a customized length to your cached information and likewise a share threshold, as proven within the code snippet beneath.
choices => choices.SetDuration(TimeSpan.FromMinutes(1))
choices => choices.SetEagerRefresh(0.5f)
Observe how the cache length has been set to 1 minute whereas the keen refresh threshold is ready to 50% of the cache length. When a brand new request arrives and your cached information is older than 50% of the cache length (i.e., after 31 seconds), FusionCache will return the cached information after which refresh the cache within the background to make sure that the cache is up to date.