我在看一个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 和延迟类。这里是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.
更多推荐
内存缓存或并发字典?
发布评论