为什么输出缓存不适合我的ASP。净MVC 4应用程序?

时间:2022-11-04 04:14:34

I am having an issue where output caching doesn't appear to be working for my ASP.NET MVC 4 (EPiServer 7) website.

我有一个问题,输出缓存似乎不适合我的ASP。NET MVC 4 (EPiServer 7)网站。

I have the following output cache profile in my web.config:

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

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

And here is my output caching configuration for static resources:

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

<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>

And my controller is decorated with an output cache attribute like so:

我的控制器是用一个输出缓存属性来装饰的:

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

I'm watching the following counters in perfmon but not seeing them increment as expected when I visit the home page:

我正在查看perfmon中的下列计数器,但是当我访问主页时,没有看到它们像预期的那样增加:

  • \ASP.NET Apps v4.0.30319(__Total__)\Output Cache Entries
  • \ ASP。NET应用程序v4.0.30319(__Total__)\输出缓存条目。
  • \ASP.NET Apps v4.0.30319(__Total__)\Output Cache Hits
  • \ ASP。NET Apps v4.0.30319(__Total__)\输出缓存命中

I've also been testing using tinyget like so:

我也用tinyget测试过

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

Any advice would be greatly appreciated!

如有任何建议,我们将不胜感激!

1 个解决方案

#1


17  

So, it turns out that OutputCaching was working, it was just that my method of testing it was flawed. The result of an action will only be cached if the response doesn't include a cookie. Of course the first response always includes a cookie if you have ASP.NET Session enabled which we do. Therefore the first response headers look like this:

结果是outputcache起作用了,只是我的测试方法有缺陷。只有当响应不包含cookie时,才会缓存操作的结果。当然,如果您有ASP,第一个响应总是包含一个cookie。NET会话启用了。因此,第一个响应头是这样的:

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

Assuming your browser or test tool can accept cookies and include those in the subsequent requests, the next request to the same page would result in HTTP response headers like so:

假设您的浏览器或测试工具可以接受cookie并在后续请求中包含这些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

As there is no client specific information in the response the output can now be cached as expected.

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

So, lesson is when testing output caching use a testing tool that can accept and return cookies in subsequent requests.

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

We ended up using Jmeter rather than tinyget and everything now works as expected.

我们最终使用了Jmeter而不是tinyget,现在一切都按照预期运行。

#1


17  

So, it turns out that OutputCaching was working, it was just that my method of testing it was flawed. The result of an action will only be cached if the response doesn't include a cookie. Of course the first response always includes a cookie if you have ASP.NET Session enabled which we do. Therefore the first response headers look like this:

结果是outputcache起作用了,只是我的测试方法有缺陷。只有当响应不包含cookie时,才会缓存操作的结果。当然,如果您有ASP,第一个响应总是包含一个cookie。NET会话启用了。因此,第一个响应头是这样的:

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

Assuming your browser or test tool can accept cookies and include those in the subsequent requests, the next request to the same page would result in HTTP response headers like so:

假设您的浏览器或测试工具可以接受cookie并在后续请求中包含这些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

As there is no client specific information in the response the output can now be cached as expected.

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

So, lesson is when testing output caching use a testing tool that can accept and return cookies in subsequent requests.

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

We ended up using Jmeter rather than tinyget and everything now works as expected.

我们最终使用了Jmeter而不是tinyget,现在一切都按照预期运行。