Dot-Net

HTTP 處理程序和 SetValidUntilExpires 中的輸出記憶體

  • June 9, 2010

我通過以下方式在我的自定義 HTTP 處理程序中使用輸出記憶體:

   public void ProcessRequest(HttpContext context)
   {
       TimeSpan freshness = new TimeSpan(0, 0, 0, 60);
       context.Response.Cache.SetExpires(DateTime.Now.Add(freshness));
       context.Response.Cache.SetMaxAge(freshness);
       context.Response.Cache.SetCacheability(HttpCacheability.Public);
       context.Response.Cache.SetValidUntilExpires(true);
       ...
   }

它可以工作,但問題是儘管有最後一個程式碼行,但使用 F5 刷新頁面會導致頁面重新生成(而不是記憶體使用):

context.Response.Cache.SetValidUntilExpires(true);

有什麼建議麼?

UPD:似乎問題的原因是 HTTP 處理程序響應沒有在伺服器上記憶體。以下程式碼適用於 web-form,但不適用於處理程序:

       Response.Cache.SetCacheability(HttpCacheability.Server);

在伺服器上記憶體 http 處理程序響應是否有一些細節?

我找到了原因。查詢字元串參數在我的 URL 中使用,所以它看起來像“ http://localhost/Image.ashx?id=49 ”。我認為如果 VaryByParams 未明確設置,伺服器將始終考慮 id 參數的值,因為 context.Response.Cache.VaryByParams.IgnoreParams 預設為 false。但實際上,在這種情況下,伺服器根本不使用記憶體(儘管使用者瀏覽器使用)。

因此,如果在查詢字元串中使用參數,則應顯式設置 Response.Cache.VaryByParams,例如

context.Response.Cache.VaryByParams.IgnoreParams = true;

忽略參數或

context.Response.Cache.VaryByParams[<parameter name>] = true;

對於某些參數的變化或

context.Response.Cache.VaryByParams["*"] = true;

所有參數的變化。

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