title: How to store nested lists of objects using MemoryCache in dotnet
body: <p>I'm trying to find the best way to store complex objects in a MemoryCache (specifically Microsoft.Extensions.Caching.Memory.MemoryCache).</p>
  <p>My data's structure would be something as below:</p>
  <pre><code>public class Foo
  {
      public Guid Id { get; set; }
      public IList&lt;Bar&gt; Bars { get; set; }
  }
  
  public class Bar
  {
     public Guid Id { get; set; }
     public Guid ParentFooId { get; set; }
  }
  </code></pre>
  <p>From an API call, I get some Foo instance with some Bars inside. It will be registered to a MemoryCache instance using:</p>
  <pre><code>CacheInstance = new MemoryCache();
  
  CacheInstance.Set(fooInstance.Id, fooInstance);
  
  //At some later point:
  var cached = CacheInstance.Get&lt;Foo&gt;(foo.Id);
  </code></pre>
  <p>After this call, <code>cached</code> will be <code>Foo</code>, but <code>cached.Bars</code> will be empty.</p>
  <p>Is there any way to store Foo an then retrieve it keeping the Bars inside?</p>
  <p><strong>[EDIT - SOLVED]</strong></p>
  <p>With the hints on the comments and the kind working example, I was able to track the issue.</p>
  <p>There was another class <code>Zee</code> at some other point in the application.</p>
  <p>As you might be imagining, it looks like:</p>
  <pre><code>public Class Zee
  {
      public Guid Id { get; set; }
      public IList&lt;Guid&gt; FooIds { get; set }
  }
  
  </code></pre>
  <p>When an instance of Zee was being cached, it was also running the FooIds and retrieving them from the API, but with an endpoint that would not include Bars.</p>
  <p>As one might expect, the <code>Foos</code> present in cache where being overwritten by these incomplete versions.</p>
  <p>Since all this procedure was going on with async calls, it was a little though to pinpoint &quot;who&quot; was overwriting.</p>
  <p>Again, thank you very much for clearing my doubt about the MemoryCache mechanics and pointing me in the right direction!</p>