2011-09-26 109 views
4

我已經創建了一個ashx的文件,以動態生成圖像縮略圖。我想在第一次調用這些圖像後緩存這些圖像。緩存ASHX圖像響應

網址:

〜/ image.ashx DIR =用戶& W = 25 & H = 25 &力= YES & IMG = matt.jpg

代碼背後:

public void ProcessRequest (HttpContext context) { 
    TimeSpan refresh = new TimeSpan(0, 15, 0); 
    context.Response.Cache.SetExpires(DateTime.Now.Add(refresh)); 
    context.Response.Cache.SetMaxAge(refresh); 
    context.Response.Cache.SetCacheability(HttpCacheability.Server); 
    context.Response.CacheControl = HttpCacheability.Public.ToString(); 
    context.Response.Cache.SetValidUntilExpires(true); 

    string dir = context.Request.QueryString["dir"]; 
    string img = context.Request.QueryString["img"]; 
    bool force = context.Request.QueryString["force"] == "yes"; 
    double w = 0; 
    double h = 0; 
    ... 
    context.Response.ContentType = "image/jpeg"; 
    thumb.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
} 

使用開發工具在Chrome中我可以看到下面的響應頭:

HTTP/1.1 200 OK 
Server: ASP.NET Development Server/10.0.0.0 
Date: Mon, 26 Sep 2011 19:17:31 GMT 
X-AspNet-Version: 4.0.30319 
Set-Cookie: .ASPXAUTH=...; path=/; HttpOnly 
Cache-Control: no-cache 
Pragma: no-cache 
Expires: -1 
Content-Type: image/jpeg 
Content-Length: 902 
Connection: Close 

有人可以告訴我,爲什麼這是不完全相同的網址多次調用緩存嗎?任何提示將非常感謝。

回答

5

當然這行:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Server); 

應該是:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public); 
+0

謝謝主席先生,我相信做到了。 – Greg

+0

如何清除此緩存呢? –

+0

@SubinJacob - 這些是緩存標題。你在說什麼? – Oded