【问题标题】:ASP.NET Caching appears to be very limited after Windows Creators UpdateWindows Creators Update 之后,ASP.NET 缓存似乎非常有限
【发布时间】:2017-09-11 05:42:25
【问题描述】:

我不确定我是否有其他具体问题——其他人看到了吗?如果是这样,是否有已知的解决方法/修复?我在 Google 上找不到任何关于此主题的内容。

基本上,我们有一个在 localhost 上运行的 ASP.NET MVC 应用程序,它在内存缓存中广泛使用 ASP.NET。我们缓存了许多对 Db 的重复请求。

昨天,我们将两台开发机器升级到了 Windows 10 Creators Update。在那次更新之后,我们注意到这些机器上的页面请求开始爬行。每页超过 30 秒。

经过一些调试和查看日志后,我们看到系统向 Db 发出相同的请求,每个请求 200-300 次。以前,这只会在第一次被缓存,并且该请求不会再次发生,直到缓存过期。

我们看到的是这段代码:

var myObject = LoadSomethingFromDb();
HttpRuntime.Cache.Insert("test", myObject);
var test = HttpRuntime.Cache.Get("test");

在某些时候,Get 将返回 NULL,即使它正好在代码中的 Insert 之后,即使缓存不可能接近满。应用程序刚刚启动。

其他人看到了吗?

【问题讨论】:

    标签: asp.net-mvc caching


    【解决方案1】:

    没关系。我们被 Absolute Cache Expiration 参数所困扰,我忽略了包含在问题代码中,因为我认为这无关紧要。

    我们使用的绝对缓存过期时间为:

    DateTime.Now.AddMinutes(60)
    

    相反,我们应该使用:

    DateTime.UtcNow.AddMinutes(60)
    

    不知道为什么前者在 Creator's Update 之前在 Windows 中很好,但对 UtcNow 的更改似乎使缓存再次工作。

    【讨论】:

    • 我也有同样的问题。在创作者更新之前,如果我使用现在可以正常工作,在创作者更新之后我需要使用 UtcNow 才能工作。问题是,如果我更改为 UtcNow 并在更新之前运行代码,缓存将会有更多我想要的时间。例如,我的区域时间是 -3 GMT,所以如果我在更新前使用 DateTime.UtcNow.AddHour(1),缓存将存活 4 小时而不是 1 小时。
    【解决方案2】:

    似乎在windows创建者更新后cache.Insert重载方法的行为有所不同。

            [Test]
        public void CanDemonstrateCacheExpirationInconsistency()
        {
            var cache = HttpRuntime.Cache;
            var now = DateTime.Now;
            var key1 =$"Now{now.Ticks}";
            var key2 = key1+"2";
            var key3 = $"UtcNow{now.Ticks}";
            var key4 = key3 + "2";
            cache.Insert(key1, true, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
            cache.Insert(key2, true, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration,CacheItemPriority.Default,null);
            cache.Insert(key3, true, null, DateTime.UtcNow.AddHours(1), Cache.NoSlidingExpiration);
            cache.Insert(key4, true, null, DateTime.UtcNow.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
    
            Assert.That(cache.Get(key1), Is.Null); //Using this overload with datetime.now expires the cache immediately
            Assert.That(cache.Get(key2), Is.Not.Null);
            Assert.That(cache.Get(key3), Is.Not.Null);
            Assert.That(cache.Get(key4), Is.Not.Null);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多