What are the compelling reasons to use a MemoryCache over a plain old Dictionary

后端 未结 1 506
说谎
说谎 2021-02-03 18:10

I 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
相关标签:
1条回答
  • 2021-02-03 18:34

    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 讨论(0)
提交回复
热议问题