.Net 4 Memory Cache Class and User Session

The new MemoryCache class in .Net 4.0 looks just like asp.net caching. My questions:

Is MemoryCache equivalent to store the object / value for the user in the session cache, but not in the code behind the aspx page.

Can I access the event of a webpage stored in MemoryCache that exists on the server?

+4
source share
2 answers

Is MemoryCache equivalent to store object / value for user in session cache

No, this is not equivalent. An ASP.NET Session object is for storing a user key / value, while MemoryCache is an application-level key / value storage (values ​​are shared among all users).

Can a value stored in MemoryCache that exists on the server access the webpage event?

ASP.NET MVC usually does not have web page events, but you can access values ​​stored in MemoryCache anywhere in the application.

Basically, in an ASP.NET application, the new MemoryCache object is just a wrapper for the old HttpContext.Cache object (it stores the values ​​in the old Cache object).

+11
source

The user session state is related to the web world, and the new MemoryCache is a new implementation that now generalizes caching availability for other types of applications, for example, a console application, winform applications, etc. MemoryCache is stored by the wrt domain of the application in which its instance was created, and is the application for all users accessing the application. Quote from this MSDN link:

The main differences between the Cache and MemoryCache classes are that the MemoryCache class has been modified to make it usable by .NET. Framework applications that are not ASP.NET applications. For example, the MemoryCache class has no dependencies on the System.Web assembly. Another difference is that you can create multiple instances of the MemoryCache Class for use in the same application and in the same AppDomain instance.

MemoryCache class is present in a separate assembly of System.Runtime.Caching.dll as a whole, which can be referenced

Note The MemoryCache class and System.Web.Caching.Cache are different implementations that lie in different dlls without interdependence. It's just that conceptually their behavior looks very similar to the fact that in any case they are a cache at the end of the day.

I would suggest reading this , this and this for a better understanding and some great ideas on this topic.

To answer your question:

  • To save something that has a wide application but is lightweight , use Application Status .
  • To save everything that is accessible , but resource - intensive - use Web Cache
  • To save anything that’s user-specific (usually light weight, as heavy-weight material will not scale along with the growing users of your site) - Use Session Status

While you are developing a website, an older web cache should be able to fulfill all your use cases. There may be very specialized use MemoryCache where you need the new MemoryCache , but I can't think about it right now.

0
source

Source: https://habr.com/ru/post/1388703/


All Articles