缓存ASHX图像响应

本文关键字:响应 图像 ASHX 缓存 | 更新日期: 2023-09-27 18:09:37

我已经创建了一个用于动态生成图像缩略图的ashx文件。我想在客户端第一次调用这些图像后缓存它们。

URL:

~/image.ashx ? dir = user& 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

有人可以启发我,为什么这不是缓存多个调用与相同的URL?如有任何建议,不胜感激。

缓存ASHX图像响应

当然这一行:

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

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