开发者

How to add a weather info to be evalueated only once?

In a ASP.MVC (1.0) project开发者_如何学编程 i managed to get weather info from a RSS feed and to show it up. The problem i have is performance:

i have put a RenderAction() Method in the Site.Master file (which works perfectly) but i 'm worried about how it will behave if a user clicks on menu point 1, after some seconds on menu point 2, after some seconds on menu point 3, .... thus making the RSS feed requesting new info again and again and again!

Can this somehow be avoided? (to somehow load this info only once?)

Thanks in advance!


You could use the server cache:

public ActionResult Index()
{
    // try getting the weather from the cache
    object weather = HttpContext.Cache.Get("weather");

    if (weather == null)
    {
        // weather not cached => fetch it from RSS
        weather = Repository.FetchWeather();

        // store in cache for the 5 next hours
        HttpContext.Cache.Add(
            "weather", 
            weather, 
            null, 
            DateTime.Now.AddHours(5), 
            Cache.NoSlidingExpiration, 
            CacheItemPriority.Normal, 
            null
        );
    }
    return View(weather);
}


I think if I were writting this app I'd download the weather details and store them in a db or xml file locally and read from that.

Weather doesn't change that much that you need minute by minute updates unless you're the weather channel or something.

Alternatively you could write this as a thread and have it report back when it's done loading. VS 2010 makes this a piece of cake btw.

So to solve this might be to give your code the ability to cancel a request and to do that you can't run this as a single threaded application. You are going to need to have the ability to kick off a request on a thread and cancel that on another request.

Faced with that I'd host it locally and run a process every say hour to update the data from the RSS feed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜