;

Cache and CacheHelper

First, cache is the fastest storage you have access to, use it. Sometimes you access the same data twice or more in the same request, if you don't use cache you are going to access the database for the same data multiple times. Each request is going to affect the database performance and make the request slower. I try to cache "everything", especially data which are common for all users, and especially if it is data which needs more than one request to your datastorage.

The simplest way is to in startup.cs add services.AddMemoryCache(); in your ConfigureServices() method. This gives you access to the IMemoryCache interface in your controllers and that's all wonderful, but it also might put you in a mess if you don't keep track of all the different keys used to access the cache, also if you decide to scale your app to multiple servers, you might start experience inconsistency in your cache.

Use a handler

Therefore, I always wrap my cache in an handlerclass. With a handler you could implement a distributed cache without having to change anything but the handler class. You can decide which cache you still want to keep local and what you want to distribute to a shared cache, Redis etc.

It also simplifies the use of slidingexpiration and sizelimitation

// Interface
public interface ICacheHandler {
    UserModel GetUser(string id);
    void SetUser(string id, UserModel model);
    void RemoveUser(string id);
}

// Handler
public interface CacheHandler {
    private readonly IMemoryCache _cache;
    public CacheHandler(IMemoryCache cache){
        _cache = cache;
    }

    private string GetUserKey(id) { return "user:" + id; }

    public UserModel GetUser(string id) {
        var key = GetUserKey(id);
        return _cache.Get<UserModel>(key);
    }
    public void SetUser(string id, UserModel model) {
        var key = GetUserKey(id);
        return _cache.Set(key, model, DateTimeOffset.UtcNow.AddHours(1));
    }
    public void RemoveUser(string id) {
        var key = GetUserKey(id);
        return _cache.Remvoe(key);
    }
}

Now you implement all of your cache methods in one place, and keep track of all your cachekeys in one place, you still might want to use the IMemoryCache, but mostly you will probably use your handler.

You add your handler in your startup.cs, and then use it in your controllers.

public void ConfigureServices(IServiceCollection services) {
     // ......
     services.AddMemoryCache();

     services.AddSingleton<ICacheHandler>(factory => {
          var cache = factory.GetRequiredService<IMemoryCache>();
          return new CacheHandler(cache);
     });
     // ......
}

For more information, read this from the horse's mouth: Cache in-memory in ASP.NET Core