Invisible caching in ASP.Net MVC
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开发者_StackOverflow社区 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.
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];
}
精彩评论