Output Caching with Custom HTTP Handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cramer

    #1

    Output Caching with Custom HTTP Handler

    I'm creating a custom handler that does not extend Page (only implements
    IHttpHandler) - and I would like for its output to be cached to improve
    runtime performance. I would like to leverage ASP.NET's output caching
    capabilities that are already in place for standard Page requests. How can I
    leverage the existing output caching mechanisms with my custom HTTP handler?
    I'd like the standard output caching options to be available, if possible
    (options like sliding expiration, etc).

    Thanks.



  • John Smith

    #2
    Output Caching with Custom Handlers

    Cramer,
    You can utilize output caching on a handler, however you have to do it via code instead of declaratively. If you add this block of code inside the “ProcessRequest ” method your handler response will be cached.

    TimeSpan freshness = new TimeSpan(0, 0, 5, 0);
    DateTime now = DateTime.Now;
    context.Respons e.Cache.SetExpi res(now.Add(fre shness));
    context.Respons e.Cache.SetMaxA ge(freshness);
    context.Respons e.Cache.SetCach eability(HttpCa cheability.Serv er);
    context.Respons e.Cache.SetVali dUntilExpires(t rue);

    You can test it out pretty easily – write a handler which returns the current time

    context.Respons e.ContentType = "text/plain";
    context.Respons e.Write("Hello World " + DateTime.Now.To LongTimeString( ));

    And run the handler without the caching code a few times. The seconds will update (as you would expect). Add that block of code above and run it again, voila – the time won’t change, its serving the page out of the output cache now.
    It supports all the same methods the declarative approach does and affords you even more control.

    context.Respons e.Cache.VaryByP arams["C"] = true;

    Comment

    • Cramer

      #3
      Re: Output Caching with Custom Handlers

      Very good! Thanks! Since the OP I was making progress, but your sample code
      shows how simple it can be.

      -Cramer


      <John Smithwrote in message news:2008591911 36eamonn.fannin g@gmail.com...
      Cramer,
      You can utilize output caching on a handler, however you have to do it via
      code instead of declaratively. If you add this block of code inside the
      "ProcessRequest " method your handler response will be cached.
      >
      TimeSpan freshness = new TimeSpan(0, 0, 5, 0);
      DateTime now = DateTime.Now;
      context.Respons e.Cache.SetExpi res(now.Add(fre shness));
      context.Respons e.Cache.SetMaxA ge(freshness);
      context.Respons e.Cache.SetCach eability(HttpCa cheability.Serv er);
      context.Respons e.Cache.SetVali dUntilExpires(t rue);
      >
      You can test it out pretty easily - write a handler which returns the
      current time
      >
      context.Respons e.ContentType = "text/plain";
      context.Respons e.Write("Hello World " + DateTime.Now.To LongTimeString( ));
      >
      And run the handler without the caching code a few times. The seconds will
      update (as you would expect). Add that block of code above and run it
      again, voila - the time won't change, its serving the page out of the
      output cache now.
      It supports all the same methods the declarative approach does and affords
      you even more control.
      >
      context.Respons e.Cache.VaryByP arams["C"] = true;
      >
      >


      Comment

      Working...