Asp.net-Mvc

為什麼輸出記憶體不適用於我的 ASP.NET MVC 4 應用程序?

  • November 2, 2014

我遇到了一個問題,即輸出記憶體似乎不適用於我的 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

任何建議將不勝感激!

所以,事實證明 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,現在一切都按預期工作了。

引用自:https://stackoverflow.com/questions/20027813