Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is System.Web.Caching or System.Runtime.Caching preferable for a .NET 4 web application

I am adding caching to an ASP.NET web application. This is .NET 4, so I can use the classes in the System.Runtime.Caching namespace (which, as I understand it, was added to provide similar functionality to that found in System.Web.Caching, but for non-Web-apps.)

But since this is a web app, am I better off using System.Web.Caching? Or is the newer System.Runtime.Caching superior in some way?

like image 963
Tim Goodman Avatar asked Jun 07 '12 17:06

Tim Goodman


People also ask

What is runtime cache?

Runtime caching refers to gradually adding responses to a cache "as you go". While runtime caching doesn't help with the reliability of the current request, it can help make future requests for the same URL more reliable.

What is .NET cache?

Caching enables you to store data in memory for rapid access. When the data is accessed again, applications can get the data from the cache instead of retrieving it from the original source. This can improve performance and scalability.

Is MemoryCache per user?

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


1 Answers

Microsoft recommends using System.Runtime.Caching for all caching purposes. See this: http://msdn.microsoft.com/en-us/library/dd997357.aspx

Although, I have come across a couple threads where people are having issues with the MemoryCache.Default instance. After a while, it stops working properly. Any item you add using the Add or Set method does not actually get added to the cache. I tried the same and was able to reproduce this issue with explicitly calling MemoryCache.Default.Dispose() method.

Here are the links: MemoryCache Empty : Returns null after being set

http://forums.asp.net/t/1736572.aspx/1

My recommendation is to use the System.Web.Caching (HttpContext.Current.Cache)

UPDATE:

This issue has been fixed by MS. Check the accepted answer in the post below: Runtime Cache Issue Resolved

like image 94
dotnetster Avatar answered Sep 23 '22 07:09

dotnetster