需要基于 asp.net mvc 应用程序中的计时器更新单例变量
我在单例中有一个变量(自定义对象),我想根据计时器进行更新。
因此,每 30 分钟我就会从数据库中获取新值并更新此变量。 不同服务器之间的最终一致性是唯一重要的事情 - 因此,如果一台服务器由于单例计时器不同步而具有较旧的值,那不是问题。
我正在考虑使用计时器在单例构造函数中生成一个线程,并根据该计时器更新变量。
我不确定在应用程序生命周期中的哪个位置可以终止从单例启动的线程。这是正确的架构方法吗?或者,还有什么我应该做的吗?
I have a variable (custom object) in a singleton that I want to update based on a timer.
So, every 30 minutes I would get the new values from the database and update this variable.
Eventual consistency between various servers is the only thing that's important - so if one server has a bit older value because the singleton timer is not synced that isn't an issue.
I was thinking of spawning off a thread in the singleton constructor with a timer and updating the variable based on that timer.
I'm not sure where in the application lifecycle a thread started from a singleton could be terminated. Is this the correct architectural approach to this? Or, is there something else I should be doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能乍一看很简单,但可能会变得非常复杂的任务,因为使用 ASP.NET 中的重复后台任务 不建议执行。原因是,在某些不受您控制的情况下,Web 服务器可能会卸载应用程序域,例如网站上一段时间不活动、达到某些 CPU/内存阈值……当然,当您的应用程序从内存中卸载后,您可能生成来执行此后台任务的所有线程都将停止。
是的,可能还有其他方法,具体取决于您想要实现的目标(在哪里以及为什么需要这个变量以及应该如何使用它,...)。
This is something that might look easy at the first place but could become very complex task because working with recurring background tasks in ASP.NET is not something that is recommended to be done. The reason is that the application domain could be unloaded by the web server in certain circumstances that are out of your control such as a period of inactivity on the site, some CPU/memory thresholds are reached, ... Of course when your application is unloaded from memory all threads that you might have spawn to perform this background task will simply stop.
Yes, there are probably other approaches depending on what you are trying to achieve (where and why do you need this variable and how it is supposed to be used, ...).
一旦过期,为什么不根据要求进行更新呢?这样,如果不使用它,您就不会浪费时间更新它。像这样...
Why not just update it when it's requested once it's out of date? This way you don't waste time updating it if it's not being used. Like this...