views:

122

answers:

3

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?

+1  A: 

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);
}
Bob
Yeah, this will work, but it means that the warm up script is not part of the application_start, which is ideally where I'd like it to be. What if IIS gets restarted? I'm in a situation where I don't have control over the box where the website sits, so I'd like everything to be self contained in the server application.
rhizohm
I can only think of hacky type ways to get it on application start. Depending on the pages and the traffic to the site I might create a scheduled task on my local machine to ping the site on a frequent interval.
Bob
I have updated my answer to show some simple code to do a warm up from Application start. It seems like it should work, but something doesn't feel right to me.
Bob
Hmm, that could work, although it feels a bit recursive or something -- also I don't know the limitations on outbound http requests from the server. I think there's a throttle there. Will investigate and respond.
rhizohm
A: 

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) 
{

}
Jack Marchetti
Well, in this scenario, I happen know what they are going to request.Yes, I know I can add the cache variable, but I want to add to the outputcache.
rhizohm
As he mentioned, he's looking to prepopulate the cache with a set of common values--things he knows will be requested. This way, when a user actually hits the site with that request, that user doesn't experience a lag while the item gets fetched and cached.
StriplingWarrior
A: 

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

pattermeister