title: .Net MemoryCache Not Working! Cache is getting invalidated and re-populated per Call
body: <p>I am not sure if this behavior is due to the nature of the application (Console application). My end goal is to use The <code>System.Runtime.Caching.MemoryCache</code> class in a Class Library that will be consumed in an ASP.Net MVC application. The goal is to return data from a MemoryCache that gets populated everytime an XML file (data source) changes on a network folder.</p>
  
  <p>So to do my implementation, I wrote a simple console app with a <code>List&lt;&gt;</code> of objects that will be cached. Here's the code. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Runtime.Caching;
  using CachePersons.Core.Logging;
  
  namespace CachePersons
  {
      class Program
      {
          static void Main(string[] args)
          {
              GetPersons();
              GetPersons();
              GetPersons();
  
              Console.ReadKey();
          }
  
          static List&lt;string&gt; GetPersons()
          {
              List&lt;string&gt; persons;
  
              Log.Debug("Entered GetPersons()");
              Console.WriteLine("Entered GetPersons()");
  
              //get default cache
              ObjectCache cache = MemoryCache.Default;
  
              //get persons
              persons = (List&lt;string&gt;)cache.Get("Persons");
  
              //if cache does not contain the persons, create new list and add it to cache
              if (persons == null)
              {
                  persons = GetPersonsFromDatabase();
  
                  cache.Add("Persons", persons, new CacheItemPolicy());
              }
              else
              {
                  Log.Debug("    Found Data in Cache!");
                  Console.WriteLine("    Found Data in Cache!");
              }
  
              Log.Debug("Exited GetPersons()");
              return persons;
          }
  
          static List&lt;string&gt; GetPersonsFromDatabase()
          {
              Log.Debug("    Populating Cache 1st time.");
              Console.WriteLine("    Populating Cache 1st time.");
              return new List&lt;string&gt;()
              {
                  "John Doe",
                  "Jane Doe"
              };
          }
  
      }
  }
  </code></pre>
  
  <p>Then I built the project and opened 2 separate command windows, and ran then 1 after another. What I expect(ed) to see on the DebugView, Console Output, is that there will be only 1 time that the Cache will be populated, and that the 2nd .exe call will find the data in the cache, and return it from there. But that's not what's happening. See below screenshots from console and debugview.</p>
  
  <p><img src="https://i.stack.imgur.com/GnOYi.png" alt="enter image description here"></p>
  
  <p>and in DebugView...</p>
  
  <p><img src="https://i.stack.imgur.com/R71Ue.png" alt="enter image description here"></p>
  
  <p>What am I doing wrong? Is this behavior because I'm using a console app? How do I get the cache working across method calls in a class library? Also what considerations do I need to be aware of, if the same library is used in a web app (ASP.Net MVC on IIS 7.5 ).</p>
  
  <p>Thank you!</p>