在httpcontext asp.net核心上缓存对象

vsl*_*lzl 1 c# asp.net asp.net-mvc httpcontext asp.net-core-mvc

我正在尝试从.net framework MVC 5迁移到.net core 2.0 MVC

好的过去,我可以通过调用获取或设置缓存的对象

HttpContext.Current.Application.Lock();
HttpContext.Current.Application["foo"] = bar;
HttpContext.Current.Application.Lock();
Run Code Online (Sandbox Code Playgroud)

但是我无法将应用程序的这一部分迁移到.net核心。

如何在核心2.0上处理此问题?

Nig*_*888 6

HttpContext.Current.Application保留了一项功能,使应用程序更容易从ASP classic移植过来。自ASP.NET 1.0 HttpContext.Current.Cache和.NET 4.0 以来,它已被取代System.Runtime.Caching

但是,AspNetCore现在缺少所有这些缓存功能。有一个轻量级缓存选项,IMemoryCache可用于许多场景,类似于HttpContext.Current.Cache。要获得更大的可伸缩性,还有一个IDistributedCache