Priming the asp.net output cache
Asked Answered
B

2

7

Is there a way to programmatically prime the asp.net output cache? I've investigated the caching API and can't seem to find an obvious way to do this. Has anyone tried something like this? If so, what method did you use?

Balfore answered 14/10, 2011 at 15:10 Comment(3)
By prime, do you meant you want it to pre-load and cache all the pages ahead of time so the first user doesn't have to wait for pages to be created?Disloyalty
@Doozer: Yes, that's exactly what I'm after.Balfore
I've never tried it, nor seen it done before. Wondering if you could fire off another thread on Application_Start to go off and hit all your pages with WebRequests or something though.Disloyalty
C
5

I gave some thought to this last year and ended up concluding that it was not that important for the case, but if it's important for you website, all you have to do is to simply call the webpages from somewhere like Application_Start (after all code has run) event but you shouldn't stop there!

The cache will eventually expire and to avoid that you should set up some way to cache the pages again before any clients requests that page.

Make the outputcache dependent on someother object in cache and set an expiration callback.

Thus, when that cache object expires, so does your pages and you should make http requests to the pages you want to recache and so on.

I'm answering to this question, but the amount of effort and question marks I still have in my mind lead me to advise not to go through with this...

UPDATE

The only kind of dependency you may set in outputcache is sql dependency. Use it if you want, but if you would need to depend your outputcache on some other business object, then this might get very difficult. I could tell you that you could set a database object and depend your database on it and expire it yourself using some kind of timer.

Man, the longer I write the more solutions and difficulties I find! I can't write a book for something that is not worthy your precious time. Believe me you that the usefulness for this will be nearly zero.

Cabin answered 22/10, 2011 at 19:44 Comment(3)
If you'd be willing to add some example code to your answer, I will award you the bounty.Balfore
Only now I saw your comment @DanP. Regardless of receiving or not the bounty, I can't give you any source code for the simple reason I don't have any read the update.Cabin
Fabio: Thank you for updating, sometimes 'it's not a good idea' is a perfectly acceptable answer as well :)Balfore
A
1

Priming the cache is as others have suggested as easy as requesting the pages you want cached. Of course if you do this programmaticly it will only request the HTML and not all the linked resources (CSS, JavaScript, Images...) which is a good thing to avoid wasted bandwidth.

For many websites the items that are cached which consume the biggest performance penalties are common to many or all pages. For example a navigation system on a large CMS or storefront may query the database and do a bunch of rendering work which can then be cached for all pages. Also a big part of the initial load in ASP.net is when the website if first accessed and loaded into memory. Both of these issues can be addressed by even calling a single page on your site, but there is nothing stopping you from making a list of URLs and calling each one periodically.

If your cache policy is set for a 20 minutes timeout, maybe request each page once every 17-18 minutes.

Here are some resources with source code to help you get started:

Good Simple Primer on requesting web URL in C#

Website Monitoring Windows Service

Asyncronous Website Monitor

As I mentioned before, you can easily extend these to "foreach" over an array or list of URLs to be requested.

Arterialize answered 25/10, 2011 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.