Using the data cache in an API controller action method

Now,we are going to make use of the questions cache in the GetQuestion method in our API controller:

  1. 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, ...

Get ASP.NET Core 3 and React now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.