2010-06-08 40 views
7

我使用的輸出緩存在我的自定義HTTP處理程序以下列方式:輸出緩存和SetValidUntilExpires

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的形式,而不是處理程序:

 Response.Cache.SetCacheability(HttpCacheability.Server); 

是否有緩存服務器上的HTTP處理程序響應的一些具體?

回答

18

我找到了原因。 查詢字符串參數正在我的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; 

由所有參數變化。

+0

感謝您找到此解決方案,爲我提供單值查詢參數。但是當我有一個像'?id = 1&id = 2&id = 3'這樣的多值參數時,這仍然不起作用。我不知道爲什麼,但想讓別人知道它。 – Oliver 2013-05-23 16:48:01