title: Pattern for concurrent cache sharing
body: <p>Ok I was a little unsure on how best name this problem :) But assume this scenarion, you're 
  going out and fetching some webpage (with various urls) and caching it locally. The cache part is pretty easy to solve even with multiple threads.</p>
  
  <p>However, imagine that one thread starts fetching an url, and a couple of milliseconds later another want to get the same url. Is there any good pattern for making the seconds thread's method wait on the first one to fetch the page , insert it into the cache and return it so you don't have to do multiple requests. With little enough overhead that it's worth doing even for requests that take about 300-700 ms? And without locking requests for other urls</p>
  
  <p>Basically when requests for identical urls comes in tightly after each other I want the second request to "piggyback" the first request</p>
  
  <p>I had some loose idea of having a dictionary where you insert an object with the key as url when you start fetching a page and lock on it. If there's any matching the key already it get's the object, locks on it and then tries to fetch the url for the actual cache.</p>
  
  <p>I'm a little unsure of the particulars however to make it really thread-safe, using ConcurrentDictionary might be one part of it...</p>
  
  <p>Is there any common pattern and solutions for scenarios like this?</p>
  
  <p>Breakdown wrong behavior:</p>
  
  <p>Thread 1: Checks the cache, it doesnt exists so starts fetching the url</p>
  
  <p>Thread 2: Starts fetching the same url since it still doesn't exist in Cache</p>
  
  <p>Thread 1: finished and inserts into the cache, returns the page</p>
  
  <p>Thread 2: Finishes and also inserts into cache (or discards it), returns the page</p>
  
  <p>Breakdown correct behavior:</p>
  
  <p>Thread 1: Checks the cache, it doesnt exists so starts fetching the url</p>
  
  <p>Thread 2: Wants the same url, but sees it's currently being fetched so waits on thread 1</p>
  
  <p>Thread 1: finished and inserts into the cache, returns the page</p>
  
  <p>Thread 2: Notices that thread 1 is finished and returns the page thread 1  it fetched</p>
  
  <p><em>EDIT</em></p>
  
  <p>Most solutions sofar seem to misunderstand the problem and only addressing the caching, as I said that isnt the problem, the problem is when doing an external web fetch to make the second fetch <strong>that is done before the first one has cached it</strong> to use the result from the first rather then doing a second</p>