Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Cache class vs. MemoryCache class

I am creating a website on ASP.NET 4.0. I am unsure whether to use the old ASP.NET Cache class or the MemoryCache class introduced with .NET 4.0. All I can find are reasons to use MemoryCache for non-web applications, but no pros or cons when programming websites (where I obviously can use both classes).

like image 887
Jpsy Avatar asked Mar 19 '12 21:03

Jpsy


People also ask

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

Is MemoryCache set thread safe?

MemoryCache is threadsafe. Multiple concurrent threads can read and write a MemoryCache instance. Internally thread-safety is automatically handled to ensure the cache is updated in a consistent manner.

Is MemoryCache a singleton?

Note that the MemoryCache is a singleton, but within the process. It is not (yet) a DistributedCache. Also note that Caching is Complex(tm) and that thousands of pages have been written about caching by smart people.

What is MemoryCache C#?

In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you're running the same process on several servers, you will have a separate cache for each server. Persistent in-process Cache is when you back up your cache outside of process memory.


1 Answers

I am personally a bit more thrilled about MemoryCache, as it is more flexible. But, you don't have to make a firm decision today if you abstract this out a bit so you can change implementations.

The beauty here is somone has already done the work for you here.

like image 128
Gregory A Beamer Avatar answered Sep 23 '22 13:09

Gregory A Beamer