我有一个问题,输出缓存似乎不适用于我的 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>
我的 Controller 装饰有一个输出缓存属性,如下所示:
[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
任何建议将不胜感激!
最佳答案
所以,事实证明 OutputCaching 是有效的,只是我的测试方法有缺陷。仅当响应不包含 cookie 时,才会缓存操作的结果。当然,如果您启用了我们所做的 ASP.NET session ,则第一个响应始终包含一个 cookie。因此,第一个响应 header 如下所示:
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 响应 header 如下所示:
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,现在一切都按预期工作。
关于asp.net-mvc - 为什么输出缓存不适用于我的 ASP.NET MVC 4 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20027813/