内存缓存或并发字典? [英] Memory Cache or Concurrent Dictionary?

查看:113
本文介绍了内存缓存或并发字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看一个WCF服务的要求水平,实现缓存。每个请求该服务执行大量的数据库调用。想想多个数据收集器。我们需要让一个数据收集器来访问已经获取由preceding数据收集的信息。

I am looking to implement caching at a request level for a WCF Service. Each request to this service performs a large number of database calls. Think multiple data collectors. We need to allow one data collector to access the information already retrieved by a preceding data collector.

我一直在寻找通过为每个请求创建一个特定的实例使用这个新的.NET 4.0内存缓存。

I was looking to use the new .Net 4.0 Memory cache for this by creating a specific instance per request.

这是一个好主意?或者我应该简单地用一个Dictionary对象?

Is this a good idea ? Or should I simply use a Dictionary object ?

BTW:数据采集将是并行,所以将有大约锁定更复杂,但是我可以使用并发集合为该以及

BTW : The data collection is going to be in parallel, so there will be more complexities around locking but I could use concurrent collections for that as well.

推荐答案

如果你不需要某种到期的逻辑,我建议使用并发集合。您可以轻松地实现单一入口cahcing机制结合 ConcurrentDictionary 延迟类。这里是<一个href="http://$c$creview.stackexchange.com/questions/2025/extension-methods-to-make-concurrentdictionary-getoradd-and-addorupdate-thread-s">another关于懒惰和ConcurrentDictionary组合链接。

If you don't need some kind of expiration logic, I would suggest using concurrent collections. You can easily implement a single entry cahcing mechanism combining ConcurrentDictionary and Lazy classes. Here is another link about Lazy and ConcurrentDictionary combination.

如果您需要您的项目到期,那么你最好使用内置的MemoryCache和实施双重检查锁定模式以保证缓存项单一的检索。

If you need your items to expire, then you better use the built-in MemoryCache and implement double-checked locking pattern to guarantee single retrieval of cache items.

这篇关于内存缓存或并发字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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