MemoryCache.Default not available in .NET Core?

I am porting some code from .NET 4.6 with .NET Core and have encountered some problems with MemoryCache. Code 4.6 uses MemoryCache.Default to instantiate the cache, but this does not seem to be available in .NET Core. Is there any equivalent to this in .NET Core, or should I rather create my own MemoryCache as singleton code and inject it through IOC?

+13
c # memorycache dnx coreclr
source share
2 answers

Usually you use singleton IMemoryCache

 IServiceProvider ConfigureServices(IServiceCollection services){ ... services.AddMemoryCache(); ... } 

but you can also create a cache

mycache = new MemoryCache(memoryCacheOptions)

If you need to do more complex things, memoryCacheOptions can be entered via - IOptions<MemoryCacheOptions> , and you can use it

 myCustomMemoryCache = new MemoryCache(memoryCacheOptions); 

https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory

+9
source share

System.Runtime.Caching.MemoryCache and Microsoft.Extensions.Caching.Memory.MemoryCache are completely different implementations.

They are similar, but have different sets of problems / caveats.

System.Runtime.Caching.MemoryCache is an older version (4.6) and is based on ObjectCache and is usually used through MemoryCache.Default, as you described. In fact, it can be used in .Net Core through the NuGet library in the standard .Net format. https://www.nuget.org/packages/System.Runtime.Caching/

Microsoft.Extensions.Caching.Memory.MemoryCache is the new base .NET version that is commonly used in new core ASP applications. It implements IMemoryCache and is usually added to services as described above by @Bogdan

https://github.com/aspnet/Extensions/blob/master/src/Caching/Memory/src/MemoryCache.cs https://www.nuget.org/packages/Microsoft.Extensions.Caching.Memory/

+2
source share

All Articles