为什么输出缓存不能用于我的ASP.NET MVC 4应用程序?

Jam*_*esF 13 asp.net-mvc caching outputcache asp.net-mvc-4 asp.net-web-api

我遇到的问题是输出缓存似乎不适用于我的ASP.NET MVC 4(EPiServer 7)网站.

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

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="PageOutput" enabled="true" duration="300" varyByParam="*" location="ServerAndClient" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>
Run Code Online (Sandbox Code Playgroud)

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

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

我的控制器装饰有输出缓存属性,如下所示:

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

我在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
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激!

Jam*_*esF 21

因此,事实证明OutputCaching正在运行,只是我的测试方法存在缺陷.只有在响应不包含cookie时,才会缓存操作的结果.当然,如果你启用了ASP.NET Session,第一个响应总是包含一个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
Run Code Online (Sandbox Code Playgroud)

假设您的浏览器或测试工具可以接受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
Run Code Online (Sandbox Code Playgroud)

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

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

我们最终使用Jmeter而不是tinyget,现在一切都按预期工作.

  • "如果响应不包含cookie,则只会缓存操作的结果" - 您保存了我的一天.谢谢 (11认同)