【问题标题】:Can't access HttpRuntime.Cache in .NET Standard class library无法访问 .NET Standard 类库中的 HttpRuntime.Cache
【发布时间】:2019-01-11 16:51:50
【问题描述】:

我开始将我们的 .NET Framework 类库移植到 .NET Standard 2.0。马上,我遇到了一个问题,找不到与以下内容的等价物:

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

我了解 .NET Standard 项目需要支持多个平台,因此可能无法再从 System.Web 处理此类逻辑,但我需要一种方法来访问类库中的 http 缓存。我确信这是一个非常常见的请求,因为开发人员移植了他们的应用程序,有什么建议吗?

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

【问题讨论】:

标签: c# .net-standard .net-standard-2.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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 2017-07-04
    • 2017-08-02
    相关资源
    最近更新 更多