如何添加只评估一次的天气信息?

发布于 2024-09-02 05:03:34 字数 244 浏览 6 评论 0原文

在 ASP.MVC (1.0) 项目中,我设法从 RSS 提要获取天气信息并将其显示出来。我遇到的问题是性能:

我在 Site.Master 文件中放置了 RenderAction() 方法(效果很好),但我担心如果用户在菜单上几秒钟后单击菜单点 1,它将如何表现第 2 点,在菜单点 3 上几秒钟后,...从而使 RSS 提要一次又一次地请求新信息!

这可以通过某种方式避免吗? (以某种方式仅加载此信息一次?)

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2024-09-09 05:03:34

您可以使用服务器缓存:

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);
}

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);
}
哥,最终变帅啦 2024-09-09 05:03:34

我想如果我编写这个应用程序,我会下载天气详细信息并将它们存储在本地的 db 或 xml 文件中并从中读取。

天气变化不会太大,除非你是天气频道或其他什么,否则你需要每分钟更新。

或者,您可以将其编写为线程,并让它在加载完成时报告。顺便说一句,VS 2010 使这变得轻而易举。

因此,要解决这个问题,可能需要让您的代码能够取消请求,并且为此您不能将其作为单线程应用程序运行。您将需要能够在线程上启动请求并在另一个请求上取消该请求。

面对这种情况,我会在本地托管它,并每隔一小时运行一个进程来更新 RSS 提要中的数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文