iisExpress 本地 httpruntime.cache 始终为空

Dav*_*ery 3 asp.net caching httpruntime.cache iis-express

奇怪的问题在这里。在 asp.net webforms (4.5 / 4.7) 的本地开发中,我发现即使正确设置 httpruntime.Cache 始终为空。我在另一个 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) {
Run Code Online (Sandbox Code Playgroud)

和测试人员

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");
}
Run Code Online (Sandbox Code Playgroud)

所以,我想知道 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);
Run Code Online (Sandbox Code Playgroud)

Wik*_*hla 5

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

而不是DateTime.Now遵循文档并将DateTime.UtcNow. 这解决了一个常见问题,即您的机器处于“非零”时区,这会阻止缓存的内部逻辑正确管理您的到期时间。

从文档

为避免本地时间可能出现的问题,例如从标准时间更改为夏令时,请为此参数值使用 UtcNow 而不是 Now。

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