views:

19

answers:

2

I have a HttpHandler that generates a Google sitemap based on my asp.net web.sitemap. Fairly standard stuff. Except that it does some fairly heavy database work to auto-generate additional urls for Ajax tabs within pages.

All this means our DB gets hit fairly heavily if the bot hits sitemap.axd.

What we need, of course, is output caching. But how do you go about caching inside something that basically writes directly to a XmlTextWriter?

+2  A: 

The simplest answer is to write the XML to a string and store it in a static field.

SLaks
Okay two questions (1) Is that thread safe? (2) How do I get a string from a method that takes a XmlTextWriter... at the moment I'm creating the new XmlTextWriter(response.Output) so everything goes directly out to response.
Hainesy
Make a static method that returns a string, and call the method in the field's initializer. That will be thread safe.
SLaks
Make an `XmlTextWriter` around a new `StringWriter`, then call `StringWriter.ToString()`.
SLaks
Darn the simplicity and my stupidity ;-) I've not done much with writers, can you tell!? Thanks!
Hainesy
+1  A: 

You can probably find a way to make a HttpHandler do pretty close to this, but I use an aspx page instead of a handler, then just hit the aspx page during off hours when and it generates an xml file. Here is an example of how I handle mine. Since yours does lots of DB IO you will probably want to add some extra security so only you can run the page. This puts things more on your terms of when you want it run.

How-to Generate and Submit Your Sitemap to Google, Yahoo!, Bing, and Ask

An Added Benefit - this also pings the search engines to let them know your sitemap has been update.

Should at least give you the jist. Hope this helps!

damstr8