views:

140

answers:

4

I am a web designer and usually design corporate web sites which often does not require update. So I want to cache the output for one day. How can I do this?

Also any suggestions for better performance for asp.net on slow servers are accepted.

+4  A: 

You use the outputcache directive

http://www.codeproject.com/KB/web-cache/cachingaspnet.aspx

10 Tips for Writing High-Performance Web Applications

http://msdn.microsoft.com/en-us/magazine/cc163854.aspx

Bruce Adams
+4  A: 

I'd recommend you to follow this rules in order to improve performance in general

http://developer.yahoo.com/performance/rules.html

If you install the YSlow for firebug it will validate all these rules for you.

And regarding to cache in particular I recommend you to read this tutorial. Cache is a very extensive topic and it's not easy to explain everything in 10 lines :-)

http://www.mnot.net/cache_docs/#CONTROL

Specifically talking about the output cache directive for ASP NET pages, it's quite simple to use. Here you have the reference

http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx

But please take into account that is important to use the cache for pages and also for other resources as css, JS and images.

Claudio Redi
+1  A: 

There is article I wrote years ago about asp.net caching. I believe you can understand the language :

http://www.csharpnedir.com/articles/read/?id=736&title=ASP.NET%27de

Braveyard
Yes I can. But Asp.Net 4 came, probably most things will change again :)
HasanGursoy
If any thing changed in Asp.NET 4.0, it most likely wouldn't effect the abstraction part of users, maybe underlying. So you don't have to worry about it.
Braveyard
+1  A: 

Be aware that ASP.NET caching has a bug since 1.0, that can effectively disable client-side caching.
That is why the HttpCachePolicy.SetOmitVaryStar() method was added later as a workaround. Developers should use it, but unfortunately, it seems that few of them know about the above method.
In response, the behavior is changed in ASP.NET 4.0, as detailed in the ASP.NET 4 Breaking Changes list:

In ASP.NET 1.0, a bug caused cached pages that specified Location="ServerAndClient" as an output–cache setting to emit a Vary:* HTTP header in the response. This had the effect of telling client browsers to never cache the page locally.

In ASP.NET 1.1, the System.Web.HttpCachePolicy.SetOmitVaryStar method was added, which you could call to suppress the Vary:* header. This method was chosen because changing the emitted HTTP header was considered a potentially breaking change at the time. However, developers have been confused by the behavior in ASP.NET, and bug reports suggest that developers are unaware of the existing SetOmitVaryStar behavior.

In ASP.NET 4, the decision was made to fix the root problem. The Vary:* HTTP header is no longer emitted from responses that specify the following directive:

<OutputCache Location="ServerAndClient" %>

As a result, SetOmitVaryStar is no longer needed in order to suppress the Vary:* header.

In applications that specify Location="ServerAndClient" in the @ OutputCache directive on a page, you will now see the behavior implied by the name of the Location attribute's value – that is, pages will be cacheable in the browser without requiring that you call the SetOmitVaryStar method.

If pages in your application must emit Vary:*, call the AppendHeader method, as in the following example:

HttpResponse.AppendHeader("Vary","*");

Alternatively, you can change the value of the output caching Location attribute to "Server".

andras
Thanks for the informative answer. I'll start using caching in my Asp.Net 4 applications.
HasanGursoy