Memory Cache in dotnet core

MJK picture MJK · Jan 6, 2017 · Viewed 40.6k times · Source

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

using System.Runtime.Caching;
using System.Collections.Concurrent;

namespace n{
public class MyCache
{
        readonly MemoryCache _cache;
        readonly Func<CacheItemPolicy> _cachePolicy;
        static readonly ConcurrentDictionary<string, object> _theLock = new ConcurrentDictionary<string, object>();

        public MyCache(){
            _cache = MemoryCache.Default;
            _cachePolicy = () => new CacheItemPolicy
            {
                SlidingExpiration = TimeSpan.FromMinutes(15),
                RemovedCallback = x =>    
                {
                    object o;
                    _theLock.TryRemove(x.CacheItem.Key, out o);
                }
            };
        }
        public void Save(string idstring, object value){
                lock (_locks.GetOrAdd(idstring, _ => new object()))
                {
                        _cache.Add(idstring, value, _cachePolicy.Invoke());
                }
                ....
        }
}
}

Within .Net core I could not find System.Runtime.Cache. After reading the .net core In Memory Cache, I have added reference Microsoft.Extensions.Caching.Memory (1.1.0) and tried

using System.Collections.Concurrent;
using Microsoft.Extensions.Caching.Memory;

namespace n
{
    public class MyCache
    {
            readonly MemoryCache _cache;
            readonly Func<CacheItemPolicy> _cachePolicy;
            static readonly ConcurrentDictionary<string, object> _theLock = new ConcurrentDictionary<string, object>();
            public MyCache(IMemoryCache memoryCache){
                   _cache = memoryCache;// ?? **MemoryCache**; 
            }

            public void Save(string idstring, object value){
                    lock (_locks.GetOrAdd(idstring, _ => new object()))
                    {
                            _cache.Set(idstring, value, 
                              new MemoryCacheEntryOptions()
                              .SetAbsoluteExpiration(TimeSpan.FromMinutes(15))
                              .RegisterPostEvictionCallback(
                                    (key, value, reason, substate) =>
                                    {
                                        object o;
                                        _locks.TryRemove(key.ToString(), out o);
                                    }
                                ));
                    }
                    ....
            }
    }
}

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 MemoryCache.Default

_cache = memoryCache ?? MemoryCache.Default; 

Answer

mattinsalto picture mattinsalto · Mar 1, 2017

The constructor is:

using Microsoft.Extensions.Caching.Memory;

. . .

MemoryCache myCache = new MemoryCache(new MemoryCacheOptions());