在普通的老式Dictionary< string,object>上使用MemoryCache的令人信服的原因是什么? [英] What are the compelling reasons to use a MemoryCache over a plain old Dictionary<string,object>

查看:63
本文介绍了在普通的老式Dictionary< string,object>上使用MemoryCache的令人信服的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了 MemoryCache ,它是新的.NET 4。

I have just come across the MemoryCache which is new in .NET 4.

如果您要这样做,我会发现它很有用:

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


  • 限制缓存的总内存使用量

  • 对于放入缓存中的对象,有一个对象过期时间(生存时间)

在标准 Dictionary< string,object>

我有几本有关C#和.NET的书,在任何地方都没有引用。

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

MemoryCache具有逐出策略,因此它可以丢弃不再需要或不需要的条目

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.

字典不会丢失内容。

更新: MemoryC ache是​​线程安全的,并且具有AddOrGetExisting之类的方法。使用字典,您必须自己同步访问权限(或使用ConcurrentDictionary)。

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

这篇关于在普通的老式Dictionary&lt; string,object&gt;上使用MemoryCache的令人信服的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆