title: Concurrent collection to use for frequent read and rare write operations (.NET)
body: <p>I want to create a cache in my web application, which will allow the top layer (MVC) to persist some values, retrieved from underlying layers (services &amp; DB), in order to avoid  unnecessary requests to it. The data, that I want to store in cache, is needed on every page of the web site, so this cache is aimed to dramatically reduce the amount of requests. The idea is that a lot of threads will read the data from collection while one thread will clear it and retrieve new data after cache expiration. This means the collection will be used for <strong>frequent reading</strong> and <strong>rare writing</strong> operations.  </p>
  
  <p>The problem for me is to choose the appropriate class for these purposes. I have read about the set of concurrent classes from <a href="http://msdn.microsoft.com/en-us/library/system.collections.concurrent%28v=vs.110%29.aspx" rel="nofollow"><code>System.Collections.Concurrent</code></a>, introduced in .NET 4. I definitely don't need to use <code>ConcurrentQueue</code> or <code>ConcurrentStack</code>, but I have to choose between <a href="http://msdn.microsoft.com/ru-ru/library/dd267312%28v=vs.110%29.aspx" rel="nofollow"><code>BlockingCollection</code></a>, <a href="http://msdn.microsoft.com/en-us/library/dd381779%28v=vs.110%29.aspx" rel="nofollow"><code>ConcurrentBag</code></a> and <a href="http://msdn.microsoft.com/en-us/library/dd287191%28v=vs.110%29.aspx" rel="nofollow"><code>ConcurrentDictionary</code></a>.  </p>
  
  <p><code>ConcurrentBag</code> seems to be the best solution in this case. However, I read in <a href="http://blogs.msdn.com/b/pfxteam/archive/2010/01/26/9953725.aspx" rel="nofollow">this article</a> that concurrent dictionary</p>
  
  <blockquote>
    <p>... is entirely lock-free for read operations.  In this way, it’s optimized for scenarios where reading from the dictionary is the most frequent operation.</p>
  </blockquote>
  
  <p>So maybe the best solution is to use <code>ConcurrentDictionary&lt;int, MyTypeOfObj&gt;</code> instead? Or maybe I don't need concurrent type at all and simple <code>List</code> will do the job? Probably, it would do if I can somehow lock operations with it for the time of cache update. But using simple <code>lock</code> is undesirable.</p>
  
  <p>Any advices and explanations are appreciated.</p>
  
  <h1>Update</h1>
  
  <p>The cache is used to store the map points of the outlets. The set of outlets is quite stable, but there should be a UI to add them, so insert operations are really rare. It might be even easier to retrieve whole collection from the underlying layer after timeout then perform insert operations. Search is not required as well, reading means simple enumeration.</p>