MemoryCache.Default在.NET Core中不可用? [英] MemoryCache.Default not available in .NET Core?

查看:195
本文介绍了MemoryCache.Default在.NET Core中不可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从.NET 4.6到.NET Core移植一些代码,并且遇到了MemoryCache问题. 4.6代码使用MemoryCache.Default实例化缓存,但是.NET Core中似乎不可用. .NET Core中有与此等效的东西吗?还是我应该将自己的MemoryCache作为单例更新并通过IOC注入?

I'm porting some code from .NET 4.6 til .NET Core and have run into some problems with MemoryCache. The 4.6 code is using MemoryCache.Default to instantiate the cache, but this doesn't seem to be available in .NET Core. Is there any equivalent to this in .NET Core or should I rather new up my own MemoryCache as a singleton and inject it via IOC?

推荐答案

通常,您将使用单例IMemoryCache

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

但是您也可以创建缓存

mycache = new MemoryCache(memoryCacheOptions)

如果您需要做一些更复杂的事情 memoryCacheOptions可以通过-IOptions<MemoryCacheOptions>注入,您可以使用

If you need to do some more complex stuff memoryCacheOptions can be injected through - IOptions<MemoryCacheOptions> and you can use it

myCustomMemoryCache = new MemoryCache(memoryCacheOptions);

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

这篇关于MemoryCache.Default在.NET Core中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆