簡體   English   中英

緩存http處理程序.ashx輸出

[英]Caching http handler .ashx output

我創建了一個包含一些文本的圖像,對於每個客戶,圖像都包含它們的名稱,我使用Graphics.DrawString函數動態創建它,但是我不需要多次創建這個圖像,只是因為客戶名稱幾乎不會改變,但我不想將其存儲在磁盤上。

現在我在處理程序中創建圖像,即:

<asp:Image ID="Image1" runat="server" ImageUrl="~/imagehandler.ashx?contactid=1" />

緩存圖像的最佳方法是什么? 我應該緩存它創建的位圖嗎? 或者緩存我傳回的流? 我應該使用哪個緩存對象,我收集有很多不同的方法? 但輸出緩存對http處理程序不起作用嗎? 推薦的方式是什么? (我不喜歡在客戶端緩存,我在服務器方面)謝謝!

我能想到的最簡單的解決方案是在你在圖像處理程序中創建它之后,將Bitmap對象緩存在HttpContext.Cache中。

private Bitmap GetContactImage(int contactId, HttpContext context)
{
    string cacheKey = "ContactImage#" + contactId;
    Bitmap bmp = context.Cache[cacheKey];

    if (bmp == null)
    {
         // generate your bmp
         context.Cache[cacheKey] = bmp;
    }

    return bmp;
}

大衛,

你可以在處理程序上使用輸出緩存。 不是聲明性的,而是代碼背后的。 看看你是否可以使用以下代碼段。

TimeSpan refresh = new TimeSpan(0, 0, 15);
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.Add(refresh));
HttpContext.Current.Response.Cache.SetMaxAge(refresh);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Server);
HttpContext.Current.Response.Cache.SetValidUntilExpires(true);

//try out with – a simple handler which returns the current time

HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.Write("Hello World " + DateTime.Now.ToString("HH:mm:ss"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM