title: Why is the value returned from a ConcurrentDictionary always null when multiple concurrent threads are used?
body: <p>I've implemented a simple memory cache backed by the ConcurrentDictionary </p>
  
  <pre><code>public class MemoryCache 
  {
      private ConcurrentDictionary&lt;string, CacheObject&gt; _memory;
  
      public MemoryCache()
      {
          this._memory = new ConcurrentDictionary&lt;string, CacheObject&gt;();
      }
  
  
      private bool TryGetValue(string key, out CacheObject entry)
      {
          return this._memory.TryGetValue(key, out entry);
      }
  
      private bool CacheAdd(string key, object value, DateTime? expiresAt = null)
      {
          CacheObject entry;
          if (this.TryGetValue(key, out entry)) return false;
  
          entry = new CacheObject(value, expiresAt);
          this.Set(key, entry);
  
          return true;
  
      }
  
      public object Get(string key)
      {
          long lastModifiedTicks;
          return Get(key, out lastModifiedTicks);
      }
  
      public object Get(string key, out long lastModifiedTicks)
      {
          lastModifiedTicks = 0;
          CacheObject CacheObject;
          if (this._memory.TryGetValue(key, out CacheObject))
          {
              if (CacheObject.HasExpired)
              {
                  this._memory.TryRemove(key, out CacheObject);
                  return null;
              }
              lastModifiedTicks = CacheObject.LastModifiedTicks;
              return CacheObject.Value;
          }
          return null;
      }
  
      public T Get&lt;T&gt;(string key)
      {
          var value = Get(key);
          if (value != null) return (T)value;
          return default(T);
      }
  
  
      public bool Add&lt;T&gt;(string key, T value)
      {
          return CacheAdd(key, value);
      }
  }
  </code></pre>
  
  <p>and now I'm trying to test it with code that is based off of a <a href="https://ayende.com/blog/4802" rel="nofollow">blog post</a> from @ayende.</p>
  
  <pre><code>var w = new ManualResetEvent(false);
  var threads = new List&lt;Thread&gt;();
  for (int i = 0; i &lt; Environment.ProcessorCount; i++)
  {
      threads.Add(new Thread(() =&gt;
      {
          w.WaitOne();
          DoWork(i);
      }));
      threads.Last().Start();
  }
  
  w.Set();//release all threads to start at the same time
  foreach (var thread in threads)
  {
      thread.Join();
  } 
  </code></pre>
  
  <p>So DoWork calls a process that contains the singleton cache manager and in my case it's going off and authenticating from a system and returns a token. This token is then stored with a unique key (user name). Now each of these calls, in my case there are 8 cores/threads the user name is the same, let's say "BobUser:CacheKey".</p>
  
  <p>Everytime I run the code I see that 8 requests are being made because the cache Get always returns null.</p>
  
  <pre><code>var token = _cm.Cache.Get&lt;MyToken&gt;(userId);
  if (token != null) return token;
  token = base.Logon(userId, password);
  if (token != null)
  {
      _cm.Cache.Add(userId, token);
  }
  return token; 
  </code></pre>
  
  <p>Is this really because of the 8 threads interacting Exactly at the same time? If that is the case, is there a pattern to fix this concurrency issue?</p>
  
  <p>Thank you,
  Stephen</p>