title: How do I cache data in .NET?
body: <p>I am trying to get caching to work for me in .NET 4.8. So here is the scenario I am working towards. I currently have a working Web API app and when a specific GET with a certain key is used, it also then calls another API as a passthrough to get some info and pass it back to the my caller. I want to change this and have a separate engine call the outside API and then cache that info (a task will execute engine every minute). Then change my API to pull data from the cache instead of making that extra API hop.</p>
  <p>So right now I am in just a proof of concept on the data engine. I tried to store just a simple value and the program ends. Then I put in some code to pull that value and the value seems to not exist anymore from the first execution. If I have both the cache add and the cache get routines running in the same execution then the get cache works. Shouldn't it be possible to add to the cache and another program execution be able to get that same value? I haven't done this in .NET yet so thats why I need a little help. Below are my two add and get methods to the cache.</p>
  <pre><code>public class MIDASCache
      { 
          public void PushToCache(object key, string cacheidentifier, DateTime expiration)
          {
              ObjectCache cache = MemoryCache.Default;
              CacheItemPolicy policy = new CacheItemPolicy();
              policy.AbsoluteExpiration = expiration;
              cache.Add(cacheidentifier, key, policy);
          }
          public Object PullFromCache(string cacheidentifier)
          {
              ObjectCache cache = MemoryCache.Default;
              Object item = cache.Get(cacheidentifier);
  
              return item;
          }
      }
  </code></pre>
  <p>Also my first execution to add was this:</p>
  <pre><code>MIDASCache cache = new MIDASCache();
  cache.PushToCache(&quot;3x4&quot;, &quot;12cache&quot;, DateTime.MaxValue.ToUniversalTime());
  </code></pre>
  <p>My 2nd execution to pull was this:</p>
  <pre><code>MIDASCache cache = new MIDASCache();
  string val = cache.PullFromCache(&quot;12cache&quot;).ToString();
  Console.WriteLine(&quot;cache: 12cache = &quot; + val);
  Console.ReadKey();
  </code></pre>
  <p>Please let me know if I am doing something wrong or if my thinking is messed up on how .NET caching actually works. Thank you</p>