title: C# MemoryCache add to list
body: <p>Im using some cache control in my c# web-api server.</p>
  
  <p>Im using the following code:</p>
  
  <pre><code>private static MemoryCache _cache = new MemoryCache("ExampleCache");
  
  public static object GetItems(string key) {
      return AddOrGetExisting(key, () =&gt; InitItem(key));
  }
  
  private static List&lt;T&gt; AddOrGetExisting&lt;T&gt;(string key, Func&lt;List&lt;T&gt;&gt; valueFactory)
  {
      var newValue = new Lazy&lt;List&lt;T&gt;&gt;(valueFactory);
      var oldValue = _cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy&lt;List&lt;T&gt;&gt;;
      try
      {
          return (oldValue ?? newValue).Value;
      }
      catch
      {
          _cache.Remove(key);
          throw;
      }       
  }
  
  private static List&lt;string&gt; InitItem(string key) {
      // im actually fetching a list from the db..but for the sake of the example
      return new List&lt;string&gt;()
  }
  </code></pre>
  
  <p>now, everything works nicely. BUT, this time I want to update something in my db and after that, I want to update the cache control so I wont have to query my db.</p>
  
  <p>Assume im using an object which looks like this</p>
  
  <pre><code>Public class Foo{
           public string Id;
           public List&lt;string&gt; values;
  }
  </code></pre>
  
  <p>And assume <code>T</code> is <code>Foo</code> in this example</p>
  
  <p>I need to add an item to the list which is stored in the <code>_cache</code> by <code>Foo</code>'s <code>Id</code> field. I would insanely appericate if the process will be thread safe.</p>
  
  <p>TIA.</p>