Now,we are going to make use of the questions cache in the GetQuestion method in our API controller:
- First, we need to make the cache available for dependency injection so that we can inject it into the API controller. So, let's register this in the Startup class:
public void ConfigureServices(IServiceCollection services){ ... services.AddMemoryCache(); services.AddSingleton<IQuestionCache, QuestionCache>();}
We enable the ASP.NET Core memory cache and then register our cache as a singleton in the dependency injection system. This means that a single instance of our class will be created for the lifetime of the app. So, separate HTTP requests will access the same class instance and, ...