无法访问.NET Standard类库中的HttpRuntime.Cache

时间:2019-01-11 16:51:51

标签: c# .net-standard .net-standard-2.0

我开始将.NET Framework类库移植到.NET Standard 2.0。立即,我遇到了找不到以下内容的问题:

private readonly System.Web.Caching.Cache _cache = HttpRuntime.Cache;

我知道.NET标准项目需要支持多个平台,因此可能不再能够从System.Web处理类似的逻辑,但是我需要一种访问类库中的http缓存的方法。我确信这是一个非常普遍的要求,因为开发人员可以移植其应用程序,有什么建议吗?

注意:我看到了关于System.Runtime.Caching的链接,这是处理HTTP缓存的新方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用nuget包使用System.Runtime.Caching

然后使用以下方法来管理缓存

要添加:

MemoryCache.Default.Add(key, value, new CacheItemPolicy { SlidingExpiration = new TimeSpan(some hours, some minutes, some seconds) });

获取:

return MemoryCache.Default.Get(key);

要删除:

MemoryCache.Default.Remove(key);
相关问题