Hello everyone, and welcome to .NET Core Central! Today, I’m excited to introduce you to FusionCache, a powerful NuGet package that offers a facade on top of other distributed caches, along with several advanced features that can significantly simplify your caching strategy. In this post, we’ll cover the basics of using this Nuget package, and in my next video, we’ll delve into its advanced features.
What is FusionCache?
FusionCache is a versatile caching library designed to enhance the standard caching mechanisms in .NET. It supports:
- Distributed Cache: Integrates seamlessly with various distributed caching systems.
- Factory Support: Allows fetching data from databases, services, or any other sources.
- Backplane Mechanism: Ensures cache synchronization across different instances.
One of the common inconveniences with traditional caching is the need to check if a cache entry exists before adding it. This Nuget packagesimplifies this process with a single method that handles both retrieval and adding of cache entries.
Setting Up FusionCache
First, let’s set up FusionCache in a .NET project. Install the FusionCache NuGet package:
Install-Package ZiggyCreatures.FusionCache
Next, let’s create a simple example to demonstrate how to use this Nuget package.
Example Scenario: Fetching Population Data by Country
We have a method called PopulationProvider
that fetches population data from an open API. The data model is based on the JSON response from this API.
PopulationProvider Class:
using Newtonsoft.Json;
internal class PopulationResponse
{
[JsonProperty("data")]
public Population[] Data { get; set; }
}
public class Population
{
[JsonProperty("ID Nation")]
public string IdNation { get; set; }
[JsonProperty("Nation")]
public string Nation { get; set; }
[JsonProperty("ID Year")]
public long IdYear { get; set; }
[JsonProperty("Year")]
public long Year { get; set; }
[JsonProperty("Population")]
public long TotalPopulation { get; set; }
[JsonProperty("Slug Nation")]
public string SlugNation { get; set; }
}
internal interface IPopulationProvider
{
Task<Population[]?> GetPopulation();
}
internal class PopulationProvider(IHttpClientFactory httpClientFactory) : IPopulationProvider
{
public async Task<Population[]?> GetPopulation()
{
using var client = httpClientFactory.CreateClient("Population");
client.BaseAddress = new Uri("https://datausa.io/api/data?drilldowns=Nation&measures=Population");
var response = await client.GetAsync(client.BaseAddress);
var content = await response.Content.ReadAsStringAsync();
var populationResponse = JsonConvert.DeserializeObject<PopulationResponse>(content);
return populationResponse?.Data;
}
}
Configuring FusionCache:
In the Program.cs
file, configure the FusionCache and HTTP client:
using TestFusionCache;
using ZiggyCreatures.Caching.Fusion;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpClient("Population");
builder.Services.AddSingleton<IPopulationProvider, PopulationProvider>();
builder.Services.AddFusionCache().TryWithAutoSetup();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/population", async (IPopulationProvider populationProvider, IFusionCache cache) =>
{
var population = await cache.GetOrSetAsync("population", async _ =>
{
return await populationProvider.GetPopulation();
}, TimeSpan.FromSeconds(15));
return population != null ? Results.Json(population) : null;
})
.WithName("GetPopulation")
.WithOpenApi();
app.Run();
In this setup:
- AddFusionCache(): Adds FusionCache to the dependency injection container.
- TryWithAutoSetup(): Configures FusionCache with default settings.
The GetOrSetAsync
method simplifies the caching logic by combining the get and set operations into a single call.
Running the Application
Run the application and access the /population
endpoint. The first request fetches the data from the API and caches it. Subsequent requests within the cache duration (5 seconds) will return the cached data.
Output:
- The first call fetches from the API.
- Subsequent calls within 15 seconds return cached data.
[
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2022,
"year": 2022,
"totalPopulation": 331097593,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2021,
"year": 2021,
"totalPopulation": 329725481,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2020,
"year": 2020,
"totalPopulation": 326569308,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2019,
"year": 2019,
"totalPopulation": 324697795,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2018,
"year": 2018,
"totalPopulation": 322903030,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2017,
"year": 2017,
"totalPopulation": 321004407,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2016,
"year": 2016,
"totalPopulation": 318558162,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2015,
"year": 2015,
"totalPopulation": 316515021,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2014,
"year": 2014,
"totalPopulation": 314107084,
"slugNation": "united-states"
},
{
"idNation": "01000US",
"nation": "United States",
"idYear": 2013,
"year": 2013,
"totalPopulation": 311536594,
"slugNation": "united-states"
}
]
If you wait for more than 15 seconds and call the endpoint again, it will fetch new data from the API.
Distributed Cache Support
FusionCache also supports distributed caching. This feature allows caching data across multiple instances of your application, ensuring consistency. In an upcoming video, I’ll demonstrate how to configure and use distributed caching with this Nuget package.
Conclusion
This Nuget package is a powerful tool that simplifies caching in .NET applications. Its ability to handle both retrieval and setting of cache entries in a single method call makes it extremely convenient. Stay tuned for more advanced features and configurations in my next video.
If you found this introduction to FusionCache helpful, please head over to my YouTube channel give it a thumbs up, and subscribe to the channel for more insights. Thank you for watching!
Related Videos from my YouTube Channel: