473,804 Members | 3,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Jun 27 '08 #1
2 4656
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;

Jun 27 '08 #2
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.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;



Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2139
by: Martin | last post by:
Hi. I had a very frustrating afternoon and evening but I have got it all under control now so all of a sudden I am in a good mood. I want to share some insights on output caching with you lot. After looking at the use of the OutputCache directive and tinkering with it a bit I found its usability.very limited. Think of it: it is okay for static content. Well that's nice but no big deal. Static content is cheap anyway, the file system...
2
347
by: Mark | last post by:
Output caching is a wonderful thing - but it apparently makes no distinctions among users. What would be good way to output cache a page per user? In my ASP.NET Web application authenticated users see things differently than unauthenticated users. I'd like to be able to cache the pages so that the different user types see output only relevant to their security access level. Any ideas? Thanks.
2
1352
by: Tim Mavers | last post by:
Is there a setting in the web.config that will enable page-output caching for an entire site? All the documentation I find on output caching deals at the page or sub-page level. I saw a couple posts on Usenet that made reference to a "web.config" setting, but didn't actually state what it was. I have a lot of pages I want to cache and would like to set it once instead of each page. Thanks,
0
1101
by: thinkfr33ly | last post by:
Is there any way, either via an ASHX file or lines in a code behind, to enable output caching for an HTTP Handler? This is driving me nuts. Nothing I try works. I've tried this in the ProcessRequest method, but it doesn't appear to do anything: context.Response.Cache.VaryByParams = true; context.Response.Cache.SetExpires(DateTime.Now.Add(Configuration.Settings.OutputHandlerCacheTimeout)); ...
2
2187
by: aptenodytesforsteri | last post by:
I have an ASP.NET 2.0 application I've localized to English, French, German, and Italian. I used resource (.resx) files. Most of the site is static content, easily 90% of it, so I thought output caching would be useful. I implemented output caching at 60 seconds.
4
15136
by: Harry Haller | last post by:
What's wrong with this: Error 3 'System.Web.Caching.Cache' is a 'type', which is not valid in the given context public List<AssetSummaryGetAssetSummary() { return (List<AssetSummary>)System.Web.Caching.Cache("assetSummary"); }
0
1387
by: Jordan S. | last post by:
Using .NET 3.5... in a "plain old" .aspx page I have the following code in the Init event: this.Context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); this.Context.Response.Cache.SetCacheability(HttpCacheability.Public); this.Context.Response.Cache.SetValidUntilExpires(false); It works great. Testing shows that the page is cached for 60 seconds as expected. Specifically, I insert the current date/time into the Response...
7
1605
by: Mark B | last post by:
How do I have a SiteMap based on the output of a stored procedure? (I want to pass a @LanguageCode parameter to it so it will display the top Menu in the appropriate language).
2
1841
by: Ken Fine | last post by:
I have a question about ASP.NET output caching. I want to use screen scraping as a temporary hack to pull in some complex Classic ASP-rendered content into some ASP.NET pages: protected String ReadHtmlPage(string url) { WebResponse objResponse; WebRequest objRequest = System.Net.HttpWebRequest.Create(url); objResponse = objRequest.GetResponse();
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.