Reputation: 1702
System.Runtime.Caching.MemoryCache is described as a kind of System.Web.Caching.Cache for use in non-web applications, but what's the difference? Is there a reason not to use System.Runtime.Caching.MemoryCache in an ASP.Net application?
Upvotes: 3
Views: 2615
Reputation: 9030
I believe System.Runtime.Caching
is actually newer, and is modeled after System.Web.Caching
, the difference being that you don't have to depend on the System.Web assembly. Also, System.Runtime.Caching.MemoryCache
is extensible, whereas you cannot inherit from System.Web.Caching.Cache
My suggestion is that if you have a common library that is, say, used for both web AND a service (for example), go with System.Runtime.Caching
so you don't have to include the System.Web fluff (if you don't need it).
Here's some discussion on the topic: dotnet System.Web.Caching.Cache vs System.RUntime.Caching.MemoryCache
MSDN: System.Runtime.Caching Namespace
MSDN: System.Web.Caching Namespace
Upvotes: 2
Reputation: 14387
The difference is that it is optimized for usage in non-asp.net application, since it has no dependancy on System.Web. According to this MSDN page, 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.
So, there is really no reason not to just use the System.Web.Caching.Cache in your asp.net application, but yes, you can use System.Runtime.Caching.MemoryCache as well.
Upvotes: 1
Reputation: 60529
Yes, you certainly can.
As for a reason not to, they aren't exactly the same thing and don't share the same cache priorities, so if you were to use both of them, then in a low memory situation you may find things getting ejected from the cache that you didn't expect.
If you just use one or the other, you should be fine.
Upvotes: 1