Jason
Jason

Reputation: 551

.Net 4 Memory Cache class and user Session

The new class MemoryCache in .Net 4.0 appears to act just like asp.net caching. My questions are:

Is MemoryCache equivalent to storing an object/value in for a user in Session Cache, but not in the code behind of an aspx page.

Can a value stored in MemoryCache, which exists on the server, be accessable to a web page event?

Upvotes: 4

Views: 4797

Answers (2)

RBT
RBT

Reputation: 25897

User session state is relevant to web world while the newer MemoryCache is a new implementation which now generalizes the availability of caching across other types of applications as well e.g. console application, winform applications etc. MemoryCache is stored w.r.t. the application domain in which its instance was created and is application to all the users accessing the application. Quoting from this MSDN link:

The main differences between the Cache and MemoryCache classes are that the MemoryCache class has been changed 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 System.Runtime.Caching.dll altogether which can be referenced

Note: The MemoryCacheclass and System.Web.Caching.Cache class are different implementations lying in different dlls with no interdependency. It is just that conceptually their behaviors look very similar as anyways they are cache at the end of the day.

I would suggest reading this, this and this thread for even better understanding and some great thoughts on this topic.

To answer your question:

  • To store anything which is application wide but light-weight - Use Application State.
  • To store anything which is application wide but resource intensive - Use Web Cache
  • To store anything which is user specific (usually light weight stuff as heavy weight stuff will not scale with growing users of your website) - Use Session state

As long as you are doing website development, the older web cache should be able to fulfill all your use-cases. There can be very specialized use cases in a weebsite where you would require the newer MemoryCachebut I can't think of any at the moment.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

Is MemoryCache equivalent to storing an object/value in for a user in Session Cache

No, it is not equivalent. The ASP.NET Session object is per user key/value storage, whereas MemoryCache is an application level key/value storage (values are shared among all users).

Can a value stored in MemoryCache, which exists on the server, be accessable to a web page event?

In ASP.NET MVC there are usually no web page events but you can access values stored in MemoryCache everywhere within the application.

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

Upvotes: 11

Related Questions