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).
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With