如何在ASP.NET中实现实时更新 [英] How to implement real time updates in ASP.NET

查看:99
本文介绍了如何在ASP.NET中实现实时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了,告诉你一个什么样的数据库回事实时更新的几个网站。一个例子可以是

I have seen several websites that show you a real time update of what's going on in the database. An example could be

  • 股票行情网站,显示股价实时
  • 在显示类似数据有什么其他用户正在搜索目前..

我想这将涉及某种形式的投票机制,查询数据库,每隔几秒钟,并在网页上呈现它。但一想到让我害怕,当我想它从性能的角度来看。

I'd assume this would involve some kind of polling mechanism that queries the database every few seconds and renders it on a web page. But the thought scares me when I think about it from the performance standpoint.

在我工作的一个应用程序,我需要显示用户已提交操作的实时状态。用户等待完成的过程。作为和当操作完成时,状态被另一个进程更新(可能是一个窗口服务)。我应该查询数据库每秒钟获得更新的状态?

In an application I am working on, I need to display the real time status of an operation that a user has submitted. Users wait for the process to be completed. As and when an operation is completed, the status is updated by another process (could be a windows service). Should I query the database every second to get the updated status?

推荐答案

它不一定在数据库完成的。当你认为是昂贵的。虽然分贝可能是一个后备存储,有可能更有效的机构用于伴随轮询操作像存储所述实时状态在存储器中除了最后的分贝。你可以比很多更有效地查询内存的从表中选择状态的每一秒。

It's not necessarily done in the db. As you suggested that's expensive. Although db might be a backing store, likely a more efficient mechanism is used to accompany the polling operation like storing the real-time status in memory in addition to finally on the db. You can poll memory much more efficiently than SELECT status from Table every second.

另外,我在评论中提到,在某些情况下,你可以得到很多里程出来锻造状态更新,通过动画,这样,采用估算的外观,不经常检查数据源。

Also as I mentioned in a comment, in some circumstances, you can get a lot of mileage out of forging the appearance of status update through animations and such, employ estimation, checking the data source less often.

(优化用于实时更低分贝资源)

而不是每个用户的数据库,以检查作业状态每X秒,稍稍改变的情况下的行为。作业被添加到数据库中每一次,读取数据库一次把元数据有关的所有在缓存中的工作。因此,举例来说,内存缓存将反映[user49 ...用户3,用户2,用户1,userCurrent] 50个用户的工作,如果1作业中的每个。 (也许我应该写它作为[job49 ... JOB2,JOB1,job_current],但同样的想法)

Instead of polling the database per user to check job status every X seconds, slightly alter the behaviour of the situation. Each time a job is added to the database, read the database once to put meta data about all jobs in the cache. So , for example, memory cache will reflect [user49 ... user3, user2, user1, userCurrent] 50 user's jobs if 1 job each. (Maybe I should have written it as [job49 ... job2, job1, job_current] but same idea)

然后各个用户的网页将轮询高速缓存它始终保持最新。在这个例子中,数据库读取仅50倍到(每一次作业提交)高速缓存。如果这些50个用户等待作业的处理和调查,平均1分钟状态每秒那么用户群调查缓存总共50个用户×60秒= 3000倍。

Then individual users' web pages will poll that cache which is always kept current. In this example the db was read just 50 times into the cache (once per job submission). If those 50 users wait an average 1 minute for job processing and poll for status every second then user base polls the cache a total of 50 users x 60 secs = 3000 times.

这是50个数据库中读取,而不是3000了一段50分钟。(平均每分钟之一。)缓存永远是新鲜的,但处理负载。这是不是考虑击中分贝每秒为每个用户少得多吓人。您可以存储其他统计数据和信息在缓存中,以帮助有估计和这样的。只要新的高速缓存提供更高的效率那么这是一个可行的替代大规模分贝命中。

That's 50 database reads instead of 3000 over a period of 50 min. (avg. one per min.) The cache is always fresh and yet handles the load. It's much less scary than considering hitting the db every second for each user. You can store other stats and info in the cache to help out with estimation and such. As long as fresh cache provides more efficiency then it's a viable alternative to massive db hits.

注:通过缓存我的意思是像应用程序或第三方解决方案,而不是ASP.NET页面缓存其中超出范围迅速的全球商店。缓存使用ASP.NET的机制,可能不适合你的情况。

Note: By cache I mean a global store like Application or 3rd party solution, not the ASP.NET page cache which goes out of scope quickly. Caching using ASP.NET's mechanisms might not suit your situation.

另注:该数据块会知道另一份工作记录追加无论来自哪里,所以触发可以初始化缓存更新。

Another Note: The db will know when another job record is appended no matter from where, so a trigger can init the cache update.

尽管一个很好的数据库解决方案,所以很多用户经常查询有可能产生问题与Web服务器的连接,你可能需要一个不同的解决方案,在这一水平,取决于交通。

Despite a good database solution, so many users polling frequently is likely to create problems with web server connections and you might need a different solution at that level, depending on traffic.

这篇关于如何在ASP.NET中实现实时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆