Search code examples
c#cachingasp.net-web-apiasp.net-core.net-core

Implementing Cache manager using Dot Net core


I am new to DotNet Core. I have created a WEB API using .NEt Core. I have a requirement to implement cache manager in this API. I have a set of record which will get updated once in a day, so i wanted this data stored in an object in memory and call database only if a change has been detected(or at a definite interval, say 3 hrs). I already tried implementing a logic in my app. but wanted to see if we have a specific packages already available !

Please help me.

Thanks in advance.


Solution

  • Asp.Net has built-in cache libraries that you can use. They are available in .Net core as well, e.g. you can read about therm here. It's a bit too long to describe here, but generally you need to register it in you services:

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

    Then you can inject IMemoryCache in your services. It requires NuGet package Microsoft.Extensions.Caching.Memory.