【问题标题】:Why is output caching not working for my ASP.NET MVC 4 app?为什么输出缓存不适用于我的 ASP.NET MVC 4 应用程序?
【发布时间】:2013-11-30 10:10:54
【问题描述】:

我遇到了一个问题,即输出缓存似乎不适用于我的 ASP.NET MVC 4 (EPiServer 7) 网站。

我的web.config 中有以下输出缓存配置文件:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="PageOutput" enabled="true" duration="300" varyByParam="*" location="ServerAndClient" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

这是我对静态资源的输出缓存配置:

<caching>
  <profiles>
    <add extension=".gif" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
    <add extension=".png" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
    <add extension=".js" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
    <add extension=".css" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="00:01:00" location="Any" />
    <add extension=".jpg" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
    <add extension=".jpeg" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="00:01:00" location="Any" />
  </profiles>
</caching>

我的控制器被这样的输出缓存属性修饰:

[OutputCache(CacheProfile = "PageOutput")]
public class HomePageController : BasePageController<HomePage>
{ ...}

我在 perfmon 中查看以下计数器,但在访问主页时没有看到它们按预期增加:

  • \ASP.NET Apps v4.0.30319(__Total__)\Output Cache Entries
  • \ASP.NET Apps v4.0.30319(__Total__)\Output Cache Hits

我也一直在使用tinyget 进行测试,如下所示:

tinyget -srv:mywebsite -uri:/ -threads:1 -loop:20

任何建议将不胜感激!

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 caching asp.net-web-api outputcache


    【解决方案1】:

    所以,OutputCaching 是有效的,只是我的测试方法有缺陷。仅当响应不包含 cookie 时才会缓存操作的结果。当然,如果您启用了我们所做的 ASP.NET 会话,那么第一个响应总是包含一个 cookie。因此,第一个响应标头如下所示:

    HTTP/1.1 200 OK
    Cache-Control: private, max-age=600
    Content-Type: text/html; charset=utf-8
    Content-Encoding: gzip
    Expires: Tue, 26 Nov 2013 03:48:44 GMT
    Last-Modified: Tue, 26 Nov 2013 03:38:44 GMT
    Vary: *
    Set-Cookie: ASP.NET_SessionId=kbnhk4lphdlcpozcumpxilcd; path=/; HttpOnly
    X-UA-Compatible: IE=Edge
    Date: Tue, 26 Nov 2013 03:38:44 GMT
    Content-Length: 9558
    

    假设您的浏览器或测试工具可以接受 cookie 并将其包含在后续请求中,则对同一页面的下一个请求将导致 HTTP 响应标头如下:

    HTTP/1.1 200 OK
    Cache-Control: private, max-age=598
    Content-Type: text/html; charset=utf-8
    Content-Encoding: gzip
    Expires: Tue, 26 Nov 2013 03:48:45 GMT
    Last-Modified: Tue, 26 Nov 2013 03:38:45 GMT
    Vary: *
    X-UA-Compatible: IE=Edge
    Date: Tue, 26 Nov 2013 03:38:45 GMT
    Content-Length: 9558
    

    由于响应中没有客户端特定信息,因此现在可以按预期缓存输出。

    因此,教训是在测试输出缓存时使用可以在后续请求中接受和返回 cookie 的测试工具。

    我们最终使用 Jmeter 而不是 tinyget,现在一切正常。

    【讨论】:

    • “只有在响应不包含 cookie 时才会缓存操作的结果” - 你救了我的命。谢谢
    猜你喜欢
    • 2012-12-25
    • 2014-10-19
    • 2017-09-28
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2015-05-05
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多