Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I Need to Call AddMemoryCache in ASP.NET Core for Cache to work?

In the latest .net Core (1.0.0-preview2-003131), I'm confused about the services that get started and also what their defaults might be.

In specific, I notice that on a new core web project, AddMvc() is called but Add AddMemoryCache() is not (both seem to be available to the app). Going one step further, it seems that in the help doc: https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory its shows AddMemoryCache.

Does AddMemoryCache() somehow get loaded someplace else? if not, why does cache seem to work if the service is not added?

like image 663
Peter Kellner Avatar asked Jan 26 '17 17:01

Peter Kellner


People also ask

How do I cache data in .NET Core?

ASP.NET Core supports several different caches. The simplest cache is based on the IMemoryCache. IMemoryCache represents a cache stored in the memory of the web server. Apps running on a server farm (multiple servers) should ensure sessions are sticky when using the in-memory cache.

How caching is done in ASP.NET explain with example?

To manually cache application data, you can use the MemoryCache class in ASP.NET. ASP.NET also supports output caching, which stores the generated output of pages, controls, and HTTP responses in memory. You can configure output caching declaratively in an ASP.NET Web page or by using settings in the Web. config file.


1 Answers

Does AddMemoryCache() somehow get loaded someplace else?

Yes. It's indirectly added when you call services.AddMvc(), as it's used in the tag helpers stack.

like image 186
Kévin Chalet Avatar answered Sep 28 '22 04:09

Kévin Chalet