By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

Output Caching with Custom HTTP Handler

P: n/a
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.

Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.Response.Cache.SetExpires(now.Add(freshnes s));
context.Response.Cache.SetMaxAge(freshness);
context.Response.Cache.SetCacheability(HttpCacheab ility.Server);
context.Response.Cache.SetValidUntilExpires(true);

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

context.Response.ContentType = "text/plain";
context.Response.Write("Hello World " + DateTime.Now.ToLongTimeString());

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.Response.Cache.VaryByParams["C"] = true;

Jun 27 '08 #2

P: n/a
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:20************************@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.Response.Cache.SetExpires(now.Add(freshnes s));
context.Response.Cache.SetMaxAge(freshness);
context.Response.Cache.SetCacheability(HttpCacheab ility.Server);
context.Response.Cache.SetValidUntilExpires(true);

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

context.Response.ContentType = "text/plain";
context.Response.Write("Hello World " + DateTime.Now.ToLongTimeString());

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.Response.Cache.VaryByParams["C"] = true;



Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.