Caching http handler .ashx output?

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. CachecacheKey; if (bmp == null) { // generate your bmp context.

CachecacheKey = bmp; } return bmp; }.

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.

CachecacheKey; if (bmp == null) { // generate your bmp context. CachecacheKey = bmp; } return bmp; }.

This worked very well for me. The images I wanted to cache were scaled versions of a file on disk. If the base image file changed, I wanted to invalidate the cache for images based on it.

I found that by adding the image to the cache with a dependency on the file everything worked perfectly: context.Cache. Insert(cacheKey,bmp,new System.Web.Caching. CacheDependency(imageFileName)) //where "imageFileName" pointed at the original file – RobbieCanuck Aug 20 '10 at 23:03 It would be much better to save the Bitmap to a MemoryStream and cache that instead.. .

1/10th the memory usage, and much better performance. Encoding a Bitmap instance to a stream is most of the CPU usage anyways - caching the Bitmap is not optimizing the bottleneck. @Robbie, if you're scaling images, look at imageresizing.

Net, disk caching is the way to go. – Computer Linguist Oct 28 at 22:14.

David, you can use output caching on handler. Not declaratively but in code behind. See if you can use following snippet.

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("Now.

ToString("HH:mm:ss")).

2 This looks like browser caching, not output caching. – William Gross Oct 6 '10 at 18:51.

Now I am creating the image in a handler i. What is the best way to cache the image that comes back? Should I cache the bitmap it creates?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions