Robert McLaws: FunWithCoding.NET

Public Shared Function BrainDump(ByVal dotNet As String) As [Value]

News

<script type="text/javascript"><!-- google_ad_client = "pub-4330602465258980"; google_hints = "ASP.NET, VB.NET, C#, C#.NET, WindowsForms, .NET Framework, VS2005, Visual Studio, XAML, WinFX, Windows Workflow, WPF, WCF, Atlas, NetFX3, Visual Studio Orcas"; google_ad_width = 120; google_ad_height = 240; google_ad_format = "120x240_as"; google_ad_type = "text_image"; google_ad_channel ="4997399242"; google_color_border = "B6C9E7"; google_color_bg = "EFEFEF"; google_color_link = "0000FF"; google_color_text = "000000"; google_color_url = "002C99"; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<!--
-->

You should feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever. That said, I will most likely only delete abusive, profane, rude, or annonymous comments, so keep it polite, please.

Blogroll

Cool .NET Articles

My .NET Tools

My Builder.com Articles

My MSKB Articles

Question on Caching an HttpHandler for Speed

Ok, I need some help with something. I'm building a HttpHandler that uses an XmlTextWriter to output some custom-generated XML to a requester. I'd like to be able to cache the generated document, because chances are good that the data won't change for a while, and I don't want to hit the DB every time. The problem is, with the following code, I don't think I can do it:

1Dim w As XmlTextWriter = New XmlTextWriter(context.Response.Output)
2w.Formatting = Formatting.Indented
3w.WriteStartDocument()
4w.WriteEndDocument()

So, what would be a better way to generate the document, store it in the cache, and output it from the cache? I'm just not familiar enough with XML in .NET to know. Thanks in advance.

 

Comments

Jim Arnold said:

Write the XML to a string first (via StringWriter, for example), and cache that. Then just stream the string back out to Response.Output.

Jim
# November 4, 2004 8:48 AM

Oleg Tkachenko said:

Or better write to a byte array (via MemoryStream) instead of string to avoid encoding issues.
# November 4, 2004 8:52 AM

BrandonFurtwangler said:

here's the general idea that i would use

if(cacheExpired)
{
//generate xml that outputs to the cache file
//not to the response
}

// stream the text from the cache file to
// the response
# November 4, 2004 9:05 AM

Ron Buckton said:

You could use output caching via Response.Cache(HttpCachePolicy) and send an ETag. That will minimize the number of actual hits to the handler
# November 4, 2004 9:46 AM

Fabrice said:

What you can do is use an ASMX instead of an HttpHandler, and set a value to the CacheDuration parameter of the WebMethod attribute.

This is what I do for RSS for example:
The URL is .../Tools.asmx/GetLatestAdditions_Rss
The method in the ASMX:
[WebMethod(CacheDuration=120)]
XmlDocument GetLatestAdditions_Rss()
{
...
}
# November 4, 2004 9:48 AM

Fabrice said:

# November 4, 2004 9:56 AM

Hector Correa said:

I am not sure what are you planning to use to store the cached documents (e.g. a HashTable.) Whatever you use, make sure you watch out for concurrency issues. For example, two requests can request the same document at the same time, and, if the document has never been cached, they *both* will try to cache it.
# November 4, 2004 10:33 AM

scottgu@microsoft.com said:

The simplest approach is to just use the Response.Cache property to control the caching policy of the output. You can do this right before the above code.

This will give you all the semantics (and more) of the <%@ OutputCache %> directive in a .aspx page. The beauty of it, though, is that you can set everything programmatically -- so you have even more control with this approach.

Hope this helps,

Scott
# November 4, 2004 10:43 AM

Robert McLaws said:

What if the resource isn't going to be pulled from a browser though? For example, what if you use a WebMethod to get the data?
# November 4, 2004 5:54 PM