ConcurrentDictionary替代可移植类库

我正在编写一个面向.NET 4.5,Windowsapp store应用程序和Windows Phone 8的可移植类库。我需要一个高效的内存缓存机制,因此我考虑使用ConcurrentDictionary ,但它不可用于WP8。

会有很多读取和相对较少的写入,所以理想情况下我想要一个支持来自多个线程的无锁读取的集合,并由单个线程写入。 根据MSDN ,非通用Hashtable具有该属性,但不幸的是它在PCL中不可用…

PCL中是否有另一个符合此要求的集合类? 如果没有,那么在不锁定读取的情况下实现线程安全的好方法是什么? (锁定写入是可以的,因为它不会经常发生)


编辑:感谢JaredPar的指导,我最终使用来自Microsoft.Bcl.Immutable的 ImmutableDictionary以完全无锁的方式实现了我的缓存:

 class Cache { private IImmutableDictionary _cache = ImmutableDictionary.Create(); public TValue GetOrAdd(TKey key, [NotNull] Func valueFactory) { valueFactory.CheckArgumentNull("valueFactory"); TValue newValue = default(TValue); bool newValueCreated = false; while (true) { var oldCache = _cache; TValue value; if (oldCache.TryGetValue(key, out value)) return value; // Value not found; create it if necessary if (!newValueCreated) { newValue = valueFactory(key); newValueCreated = true; } // Add the new value to the cache var newCache = oldCache.Add(key, newValue); if (Interlocked.CompareExchange(ref _cache, newCache, oldCache) == oldCache) { // Cache successfully written return newValue; } // Failed to write the new cache because another thread // already changed it; try again. } } public void Clear() { _cache = _cache.Clear(); } } 

要考虑的一个选项是在不可变搜索树上编写精简外观。 网上有几个不可变的搜索树可供选择。 我通常根据Eric Lipperts关于这个主题的post

  • 不可变二进制搜索树

使用它作为后备数据结构将使您无锁。 写入树也可以使用CAS以无锁方式完成。 这将比ConcurrentDictionary慢一点,因为查找是O(Log(N))而不是接近O(1)。 但它应该为你做的伎俩