4

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?

Jason
  • 551
  • 2
  • 5
  • 21

2 Answers2

11

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

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • So how would one access a value set in MemoryCache (in the controller or model level) down at the view level? – Jason Dec 31 '11 at 15:23
  • 2
    @Jason, in a properly designed ASP.NET MVC application the view shouldn't access anything else than the view model that it is passed to it by the controller. So your controller action will query the cache, build a view model and pass this view model to the view. Then the view will simply display the corresponding property. – Darin Dimitrov Dec 31 '11 at 15:26
  • Thank you for the info Darin. I realize what I was trying to accomplish with MVC and your explanation helped me figure out what I was doing wrong. – Jason Dec 31 '11 at 18:54
  • @DarinDimitrov Are you sure about this - `Basically, in an ASP.NET application, the new MemoryCache object is just a wrapper for the old HttpContext.Cache object`? The two implementations lie in different dlls and I can use either of them by referencing their respective dll. I believe `MemoryCache` is a new concept which is more generalized and can be used both in win form and web applications as mentioned [here](https://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx). Conceptually they might exhibit similar behavior but calling `MemoryCache` as a wrapper sounds odd. – RBT Jan 19 '18 at 23:08
0

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.

RBT
  • 24,161
  • 21
  • 159
  • 240