Caching http handler .ashx output

前端 未结 2 524
广开言路
广开言路 2021-02-10 19:33

Im creating an image which has some text in it, for every customer, the image contains their name and I use the Graphics.DrawString function to create this on the fly, however I

2条回答
  •  广开言路
    2021-02-10 20:06

    The simplest solution I can think of would be to just cache the Bitmap object in the HttpContext.Cache after you've created it in the image handler.

    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;
    }
    

提交回复
热议问题