iisExpress local httpruntime.cache 始终为空

iisExpress local httpruntime.cache always null

奇怪的问题。在 asp.net webforms (4.5 / 4.7) 的本地开发中,我发现即使设置正确,httpruntime.Cache 也始终为 null。我在另一个 iis express 工作站上尝试了它,发现了相同的行为,即使是测试器单页网页也是如此。生产 IIS 7.5 中的同一页面可以正常工作,并且正在从缓存中存储和传送。具体的代码在下面,但我已经尝试过一个测试器在 httpruntime.Cache 中存储一个简单的字符串。

var cache = System.Runtime.Caching.MemoryCache.Default;
var luCacheKey = "lu_" + dsName;

var ic = HttpRuntime.Cache.Get(luCacheKey) as ICollection;
if (ic == null) {

来自测试人员

var item = HttpRuntime.Cache.Get("x");
if (item == null)
{
    HttpContext.Current.Cache.Insert("x", "test" , null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
    Response.Write("added to cache<br>");
}
else {
    Response.Write("already in cache");
}

所以,我想知道 web.config 中是否有我可以查看的内容,或者这是预期的 IIS 表达行为吗?请注意,System.runtime.Caching 确实可以正常工作。

var cache = System.Runtime.Caching.MemoryCache.Default;
var ic = cache[luCacheKey] as ICollection;
if (ic == null)
{
var filterCriteria = new BinaryOperator("LookupGroup", dsName, BinaryOperatorType.Equal);
var lookups = xpoSession.GetClassInfo(typeof(Lookups));
ic = xpoSession.GetObjects(lookups, filterCriteria, new SortingCollection(), 0, 0, false, false);
var cachePolicy = new System.Runtime.Caching.CacheItemPolicy() { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(30) };
cache.Add(new System.Runtime.Caching.CacheItem(luCacheKey, ic), cachePolicy);

您错误地将对象添加到缓存中。

而不是 DateTime.Now 按照文档并输入 DateTime.UtcNow。这解决了一个常见问题,即您的计算机处于 "non-zero" 时区,这会阻止缓存的内部逻辑正确管理您的过期时间。

来自文档

To avoid possible issues with local time such as changes from standard time to daylight saving time, use UtcNow rather than Now for this parameter value.

https://msdn.microsoft.com/en-us/library/4y13wyk9(v=vs.110).aspx

添加更多信息作为跟进,说明行为可能在服务器之间发生变化的原因。

此行为变化可能是由于在计算机上安装了 .NET 4.7。下面链接的文章说 Microsoft 将在下一版本的 .NET 和下一个修补程序中修复此问题。

引用 Microsoft 页面的部分内容:

症状:

Assume that you have Microsoft .NET Framework 4.7 installed on a computer. When you try to insert items into the Cache object by using the Cache.Insert (string, object, CacheDependency, DateTime, TimeSpan) Insert overload method, you may notice that the inserted Cache items expire much earlier or later than the specified DateTime (expiration time).

原因:

The internal implementation of System.Web.Caching.Cache uses Coordinated Universal Time (UTC) time-stamp for absolute expiration. But this particular Cache.Insert (string, object, CacheDependecy, DateTime, TimeSpan) Insert overload method does not make sure whether the expiration time is converted to UTC. Therefore, expiration for items that are inserted into the Cache object by using this overload will occur earlier or later than expected, depending on the computer time zone difference from Greenwich Mean Time (GMT).

解决方法:

The temporary workaround for this issue is to use either the Cache.Add method or a different Cache.Insert overload method.

分辨率:

This issue will be fixed in the next version of the .NET Framework, and will also be available in the next hotfix for the .NET Framework 4.7.

参考文献:

https://support.microsoft.com/en-us/help/4035412/fix-expiration-time-issue-when-you-insert-items-by-using-the-cache-ins

http://vimvq1987.com/2017/08/episerver-caching-issue-net-4-7/