Dav*_*vid 4 asp.net caching httphandler
我创建了一个包含一些文本的图像,对于每个客户,图像都包含它们的名称,我使用Graphics.DrawString函数动态创建它,但是我不需要多次创建这个图像,只是因为客户名称几乎不会改变,但我不想将其存储在磁盘上.
现在我在处理程序中创建图像,即:
<asp:Image ID="Image1" runat="server" ImageUrl="~/imagehandler.ashx?contactid=1" />
Run Code Online (Sandbox Code Playgroud)
缓存图像的最佳方法是什么?我应该缓存它创建的位图吗?或者缓存我传回的流?我应该使用哪个缓存对象,我收集有很多不同的方法?但输出缓存对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;
}
Run Code Online (Sandbox Code Playgroud)