开发者

What are the compelling reasons to use a MemoryCache over a plain old Dictionary<string,object>

开发者_如何学GoI have just come across the MemoryCache which is new in .NET 4.

I get that it can be useful if you want to:

  • Limit the total memory usage of the cache
  • Have an object expiry time (time to live) for objects you put in the cache

Are there any other compelling reasons to use a MemoryCache over a standard Dictionary<string,object>

I have a few books on C# and .NET and there is no reference to it anywhere.


I think you nailed the two compelling reasons :-)

The MemoryCache has an eviction strategy, so that it can throw out entries that are no longer needed or for that you do not have enough memory anymore.

A Dictionary will not "lose contents".

Update: MemoryCache is thread-safe and has methods such as AddOrGetExisting. With a Dictionary, you'd have to synchronize access yourself (or use ConcurrentDictionary).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜