2009-07-29 134 views
0

因爲我們每個表單調用的次數多於50次。我怎樣才能夠緩存客戶端的圖像。從數據庫緩存動態生成的圖像

對於每個刷新它調用並命中數據庫。

<%@ WebHandler Language="C#" %> 

using System; 
using System.Web; 
using System.Data.SqlClient; 
using App_Code.BLL.Products; 

public class ProductPicture : IHttpHandler 
{ 
    private Product _objProducts; 
    private ProductBL _objProductBL; 

    #region IHttpHandler Members 

    public void ProcessRequest(HttpContext context) 
    { 
     string strSize = context.Request.QueryString["PhotoType"]; 
     _objProducts = new Product(); 
     _objProductBL = new ProductBL(); 

     _objProducts.PictureID = Convert.ToInt32(context.Request.QueryString["PhotoId"]); 
     SqlDataReader objReaderPhoto = _objProductBL.GetPictureByPictureID(_objProducts); 
     if (!objReaderPhoto.HasRows) return; 
     objReaderPhoto.Read(); 
     context.Response.ContentType = "Image/JPEG"; //Convert.ToString(objReaderPhoto["Photograph"]); 
     context.Response.AddHeader("Content-Disposition", "attachment; filename=Image"); 
     switch (strSize) 
     { 
      case "m": 
       context.Response.BinaryWrite((byte[])objReaderPhoto["PictureBinary"]); 
       break; 
      //case "s": 
      // context.Response.BinaryWrite((byte[]) objReaderPhoto["PhotoSearch"]); 
      // break; 
      case "t": 
       context.Response.BinaryWrite((byte[])objReaderPhoto["PictureThumbnail"]); 
       break; 
      default: 
       context.Response.BinaryWrite((byte[])objReaderPhoto["PictureBinary"]); 
       break; 
     } 

     context.Response.Cache.SetLastModified(DateTime.Now.AddYears(-1)); 

     objReaderPhoto.Close(); 
    } 

    public bool IsReusable { 
     get { 
      return false; 
     } 
    } 

    #endregion 
} 

回答

2

我不認爲你可以緩存它在客戶端上,除非它有一個網址「IMG/someimage.gif」,怎麼樣在HttpContext的緩存緩存的結果?一點幫助Cache