title: Memory Cache in dotnet core
body: <p>I am trying to write a class to handle Memory cache in a .net core class library. If I use not the core then I could write </p>
  
  <pre><code>using System.Runtime.Caching;
  using System.Collections.Concurrent;
  
  namespace n{
  public class MyCache
  {
          readonly MemoryCache _cache;
          readonly Func&lt;CacheItemPolicy&gt; _cachePolicy;
          static readonly ConcurrentDictionary&lt;string, object&gt; _theLock = new ConcurrentDictionary&lt;string, object&gt;();
  
          public MyCache(){
              _cache = MemoryCache.Default;
              _cachePolicy = () =&gt; new CacheItemPolicy
              {
                  SlidingExpiration = TimeSpan.FromMinutes(15),
                  RemovedCallback = x =&gt;    
                  {
                      object o;
                      _theLock.TryRemove(x.CacheItem.Key, out o);
                  }
              };
          }
          public void Save(string idstring, object value){
                  lock (_locks.GetOrAdd(idstring, _ =&gt; new object()))
                  {
                          _cache.Add(idstring, value, _cachePolicy.Invoke());
                  }
                  ....
          }
  }
  }
  </code></pre>
  
  <p>Within .Net core I could not find System.Runtime.Cache. After reading the .net core <a href="https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory" rel="noreferrer">In Memory Cache</a>, I have added reference Microsoft.Extensions.Caching.Memory (1.1.0) and tried</p>
  
  <pre><code>using System.Collections.Concurrent;
  using Microsoft.Extensions.Caching.Memory;
  
  namespace n
  {
      public class MyCache
      {
              readonly MemoryCache _cache;
              readonly Func&lt;CacheItemPolicy&gt; _cachePolicy;
              static readonly ConcurrentDictionary&lt;string, object&gt; _theLock = new ConcurrentDictionary&lt;string, object&gt;();
              public MyCache(IMemoryCache memoryCache){
                     _cache = memoryCache;// ?? **MemoryCache**; 
              }
  
              public void Save(string idstring, object value){
                      lock (_locks.GetOrAdd(idstring, _ =&gt; new object()))
                      {
                              _cache.Set(idstring, value, 
                                new MemoryCacheEntryOptions()
                                .SetAbsoluteExpiration(TimeSpan.FromMinutes(15))
                                .RegisterPostEvictionCallback(
                                      (key, value, reason, substate) =&gt;
                                      {
                                          object o;
                                          _locks.TryRemove(key.ToString(), out o);
                                      }
                                  ));
                      }
                      ....
              }
      }
  }
  </code></pre>
  
  <p>Hope code inside the save method is ok, although most of my mycache tests are failing at the moment. Can anyone please point out what is wrong?
  The main question is about the constructor What should i do to set the cache instead <strong><em>MemoryCache.Default</em></strong></p>
  
  <pre><code>_cache = memoryCache ?? MemoryCache.Default; 
  </code></pre>