Skip to Main Content
ASP.NET Core 3 and React
book

ASP.NET Core 3 and React

by Carl Rippon
December 2019
Intermediate to advanced content levelIntermediate to advanced
598 pages
12h 21m
English
Packt Publishing
Content preview from ASP.NET Core 3 and React

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ASP.NET Core 5 and React - Second Edition

ASP.NET Core 5 and React - Second Edition

Carl Rippon
ASP.NET Core 5 for Beginners

ASP.NET Core 5 for Beginners

Andreas Helland, Vincent Maverick Durano, Jeffrey Chilberto, Ed Price

Publisher Resources

ISBN: 9781789950229Supplemental Content