使用 SqlCacheDependency 获取实时更新? - ASP.NET
我想在网页上显示实时更新(基于由外部进程更改的数据库表中的状态字段)。根据我的研究,有几种方法可以做到这一点。
- 长轮询 (Comet) - 实现定期轮询似乎很复杂
- - 我可以使用 AJAX 方法每 5 秒触发一次数据库访问以获得当前状态。但我担心这会带来性能问题。
然后我读到了有关使用 SqlCacheDependency
的内容 - 基本上缓存根据表中的字段而失效。我假设我可以使用缓存失效时触发的事件向用户显示新的更新?
什么是不会出现性能问题的简单解决方案?
I would like to display real time updates on a web page (based on a status field in a database table that is altered by an external process). Based on my research, there are several ways of doing this.
- Long Polling (Comet) - This seems to be complex to implement
- Regular Polling - I can have an AJAX method trigger a database hit every 5seconds to get the current status. But I fear this will have performance issues.
Then I read about using SqlCacheDependency
- basically the cache gets invalidated based on a field in the table. I am assuming I can use the event trigerred when the cache is invalidated to show the new update to the user?
What's an easy solution that will not have performance issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与轮询数据库不同,更具可扩展性和性能的方法是轮询 Web 服务器上是否存在文件,例如轻量级 js 文件。
文件的内容并不重要,但为了让您了解,您可以拥有某种 JSON 对象,它更详细地描述了该过程的结果。
然后,作为最后一步进行处理的后台进程可以创建文件或调用 Web 层上的 Web 服务来执行此操作。
这是它的工作原理。
用户按下发布到服务器的按钮,
服务器启动进程并返回标识符,例如 C3201620-E622-4fe2-9F3A-E02FFA613F59
Web UI,然后定期轮询 C3201620-E622-4fe2-9F3A-E02FFA613F59.js 的存在,javascript 将管理404 错误并继续重试,直到收到 200
希望这能给您一些想法。
在 jQuery 中处理 404 的代码示例
Instead of polling the database a more scalable and performant approach would be to poll for the existence of a file on your web server, something like a lightweight js file.
The contents of the file is not important, but to give you an idea you could have a JSON object of some sort which describes in greater detail the result of the process.
Then your background process which is doing the processing as the final step can create the file or call a web service on your web tier to do so.
Here's how it could work.
Users presses button which posts to server
Server kicks of process and returns an identifier e.g. C3201620-E622-4fe2-9F3A-E02FFA613F59
Web UI then polls peridodically for the existence of C3201620-E622-4fe2-9F3A-E02FFA613F59.js, the javascript would manage the 404 error and keep retrying until it receives a 200
Hope this gives you some ideas.
Code example to handle 404's in jQuery
您有两个问题:
第 1 项很容易,假设您有一个相当有效的查询,并且您不是为每个用户查询它;只需查询、获取数据并推送即可。您可以使用 SqlDependency 选项,也可以只轮询它;这取决于您的数据有多重。
第#2 项有点棘手,因为您需要不断地访问服务器,或者使用彗星服务器。我们在 Frozen Mountain 编写了一个高度可扩展的 .NET comet 服务器,WebSync,可能适合账单。本质上,您有一个单独的流程来驱动发布,WebSync 将处理将数据推送到您的客户端。
You have 2 issues:
Item #1 is easy, assuming you have a fairly efficient query, and you're not querying it for every user; just query, get the data, and push it. You could use the SqlDependency option, or you could just poll it; it depends on how heavy your data is.
Item #2 is a little trickier, because you'll need to either hit the server constantly, or use a comet server. We've written a highly scalable .NET comet server, WebSync, over at Frozen Mountain that might fit the bill. Essentially, you have a separate process that drives the publishing, and WebSync would handle the pushing of the data to your clients.