1

I'm creating a page that has some dynamically generated images built from data that comes from web services. The image generation takes a fair amount of time due to the time involved in hitting the web services, so I need to do some caching.

One option would be to use the OutputCache parameter to cache the images, but I don't like forcing some unlucky user to wait for a really long time. I'd rather write the images to files in the background and serve static html.

Whats the best way to do this? I'm thinking about creating a special url to trigger refreshes that writes the images to disk and setting up a scheduled task of some sort to hit the refresh url. Any better ideas?

It seems its possible to use memcached with ASP.Net, how hard would that be to set up? Seems like it may be overkill for this situation (internal tool) and I've already got the disk based version working, but I'm curious.

4
  • How many of these images are we talking about? Is one of these images the main focus of one of your pages? Commented Jul 28, 2010 at 23:24
  • The page I'm working on right now has 30-40. I can't speed up the web service, so loading from scratch takes a couple minutes (inhumane to make a visitor wait this long). Commented Jul 28, 2010 at 23:26
  • Do you want them to be precomputed before the first request for a page even comes in. Or do you want to compute them only once a client loads the page that contains links to them for the first time? Commented Jul 28, 2010 at 23:57
  • I want them to be precomputed. If I want to do it the other way I could just use OutputCache. Commented Jul 29, 2010 at 17:10

1 Answer 1

1

We do something similar, although we simply precompute the files/images and store them in the HttpRuntime.Cache. This way our views can still be generated as-is, but they typically pull from cached data rather than generating on-the-fly.

On the off chance that the cached data isn't available, we have getter functions to generate them:

public static GetGraph(int id)
{
    if (HttpRuntime.Cache["image_"+id] == null)
        HttpRuntime.Cache["image_"+id] = _imageGen.GenerateGraph(id);
    return HttpRuntime.Cache["image_"+id];
}
Sign up to request clarification or add additional context in comments.

2 Comments

The issue is that I never want the user to trigger the generation -- not even once. I suppose in this method you can just have the scheduled task visit the same page the user is visiting.
@Luke - we typically don't have the user trigger the generation either. We cache everything for 30 hours, and have a process that rebuilds the cache each night. The advantage of the above message is that the image/file/etc would still be available even if the batch process didn't run for some reason ... and wouldn't necessitate a system outage to rebuild all cached objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.