Using the In-Memory Cache with .NET Core Web API

Full source code available here.

If you need to store anything temporarily within an application, you should consider using the In-Memory Cache from Microsoft. In .NET Core it is much easier to use than its predecessor from Framework 4.x. You can store almost anything in it, in a type safe manner.

When adding an item to the cache you set the an expiration, this can be an absolute time, a sliding window or the combination of both. Choosing which one to use depends on your scenario.

When an item is removed from the cache a delegate can be called, you can use this for logging or some other purpose.
Items can also be evicted when the cache is running low on memory.

In this example I’m going to show a simple usage example for the cache inside a .NET Core Web API application.
Just a single line in startup.cs adds the cache to the dependency injection container.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMemoryCache();
    services.AddMvc();
}

Now I can access the cache from inside all my controllers by just adding an IMemoryCache parameter to the constructor.

private readonly IMemoryCache _cache;
public CountriesController(IMemoryCache memoryCache)
{
    _cache = memoryCache;
}

I have a very simple GET method that takes a country code as its parameter and looks up full name of the country.

[HttpGet("{countryCode}")]
public async Task<IActionResult> Get(string countryCode)
{
    string country = await LookupCountryByCode(countryCode);

    if (string.IsNullOrEmpty(country))
    {
        return NoContent();
    }
    return Ok(country);
}

And here is where I use the cache.

private async Task<string> LookupCountryByCode(string countryCode)
{
    if(!_cache.TryGetValue("ListOfCountries", out Dictionary<string, string> countries))
    {
        Console.WriteLine("Cache miss....loading from database into cache");
        countries =
            JsonConvert.DeserializeObject<Dictionary<string, string>>(
                await System.IO.File.ReadAllTextAsync("countrylist.json"));

        _cache.Set("ListOfCountries", countries, TimeSpan.FromSeconds(10));
    }
    else
    {
        Console.WriteLine("Cache hit");
    }

    return countries[countryCode];
}

On line 3, I check if the list of countries is in the cache; I won’t be the first time through this method or if the cache entry has expired.
Inside the if clause I load the list of countries from a json file, but you could load from a database or any other source. Then I add the list to the cache and set when the entry will expire, in this example it will expire 10 seconds after it was added.

If you want to use both an absolute expiration and sliding window you need to pass the MemoryCacheEntryOptions when adding an item to the cache.

MemoryCacheEntryOptions options = new MemoryCacheEntryOptions
{
    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(25), // cache will expire in 25 seconds
    SlidingExpiration = TimeSpan.FromSeconds(5) // caceh will expire if inactive for 5 seconds
};

_cache.Set("ListOfCountries", countries, options);

Full source code available here.

Leave a Reply

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