I'm using an outputcache page directive to cache values based on VaryByParam. Is there a way I can prepopulate the cache when the web application starts up for a set of common params, instead of having to wait for a user to hit the page? Or do I just need to write a script that hits all the pages I want cached? Ideally, I can do it serverside somewhere in global.asax application_start?
In the past I have used warm up scripts
http://programmerramblings.blogspot.com/2008/09/aspnet-site-warmup.html
A solution like this will basically "ping" ever page. This "ping" will cause your cache to get activated. It will also prevent users from hitting the cold website so the pages will be fully jit'ted by the time they get there.
Edit: I still don't know how I feel about the smell of the following solution, but how about firing off a web request on application start to each page?
string[] cachedPages = new string[] { "http://...", "http://...", ...};
foreach (var url in cachePages) {
var request = WebRequest.Create(url);
request.BeginGetResponse(null, null);
}
Why would you want to cache things before someone actually requests them? Doesn't that use up resources as well?
I'm not sure if you can actually cache a page dynamically as you are saying, but you can add objects to the cache in the Global.asax file.
void Application_Start(object sender, EventArgs e)
{
}
Obviously you're keen on a solution now, but take a look at what's coming in ASP.NET 4. There are two new additions which may well help you out.
The first is the ability to specify "warm up" logic for ASP.NET apps - you configure this in the web.config and optionally tell it to run code that implements IProcessHostPreloadClient. See here for details: http://weblogs.asp.net/scottgu/archive/2009/09/15/auto-start-asp-net-applications-vs-2010-and-net-4-0-series.aspx
The second is that you'll get a proper provider model for output caching. Until now, there's only been one option: caching in the worker process. So every time there's a recycle, you lose your cache (and you are of course limited by memory constraints). In ASP.NET 4, you get disk based caching out of the box too. See here for details: http://www.asp.net/learn/whitepapers/aspnet40/#_TOC1_2