views:

2223

answers:

2

How can I use output caching with a .ashx handler? In this case I'm doing some heavy image processing and would like the handler to be cached for a minute or so.

Also, does anyone have any recommendations on how to prevent dogpiling?

+5  A: 

There are some good sources but you want to cache you processing server side and client-side.

Adding HTTP headers should help in the client side caching

here are some Response headers to get started on..

You can spend hours tweaking them until you get the desired performance

//Adds document content type
context.Response.ContentType = currentDocument.MimeType;
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
context.Response.Cache.SetMaxAge(new TimeSpan(0,10,0)); 
context.Response.AddHeader("Last-Modified", currentDocument.LastUpdated.ToLongDateString());

// Send back the file content
context.Response.BinaryWrite(currentDocument.Document);

As for server side caching that is a different monster... and there are plenty of caching resources out there...

BigBlondeViking
Thanks - from reading the comment left above its obvious that output caching can't be used directly in this situation either, so will go with a manual server side solution and this client side one.
Kieran Benton
I did a lot of image serving out of out DB for thumbnails. One thing you need to be careful is setting end user expectations... A user would update a image, then go check the live site... well because the browser was told to cache the image and not even ask for it... they wouldn't see there updates... ( then they would come crying back to dev... and we would say go clear ur browser cache...)
BigBlondeViking
Cheers man - I feel your pain, we cache a lot of different things (just not these images as it turns out - which is causing us problems!) and quite regularly you get people complaining about non-immediate changes, even if its only a few minutes timeout!
Kieran Benton
Got to have Caching, After I left that company they changed technologies, and implemented a more "real time" updating site, using open sources projects, added new servers, dbs.. and they turned it on the site slowed till it collapsed (under less daily traffic)... it was down for 3-4 days until they turn Akamai Web Caching on there entire site [ meaning... they now own a web cluster/db cluster, as well as hosting that they don't use ( but pay for ).. plus they pay Akamais' crazy rates..] f-them- rants from a bitter ex-employee ( they ruined my baby~ )
BigBlondeViking
Re: MaxAge... http://www.codeproject.com/KB/ajax/aspnetajaxtips.aspxQuote: "There's a bug in ASP.NET 2.0 where you cannot change the max-age header."
frankadelic
+1  A: 

I used the following with success and thought it worthwhile to post here .

Manually controlling the ASP.NET page output cache

From http://dotnetperls.com/cache-examples-aspnet

Setting cache options in Handler.ashx files

First, you can use HTTP handlers in ASP.NET for a faster way to server dynamic content than Web Form pages. Handler.ashx is the default name for an ASP.NET generic handler. You need to use the HttpContext parameter and access the Response that way.

Sample code excerpted:

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

C# to cache response for 1 hour

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        // Cache this handler response for 1 hour.
        HttpCachePolicy c = context.Response.Cache;
        c.SetCacheability(HttpCacheability.Public);
        c.SetMaxAge(new TimeSpan(1, 0, 0));
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}
John K