ASP.NET:应用程序生命周期、静态变量
我的 ASP.NET 网站中的一个类需要针对每个请求多次访问数据库表。数据库表应该很少改变(如果有的话)。也许每个月几次。
在类的静态构造函数中,从数据库中获取表并将其缓存在静态局部变量中。每当类需要访问表时,它就只使用缓存的静态版本。
我的问题涉及该表的缓存静态版本的生命周期。
据我所知,它是在第一次实例化类或使用类中的静态方法时获取的。但这种情况在 Web 服务器上发生的频率是多少?如果表发生变化并且我们想要重置该表的静态版本怎么办?
基本上,我想知道,这个表是否只获取一次,然后每次重新启动 IIS 时才重新获取?对于站点和IIS来说,什么会触发这个静态类重置,导致静态表被重新获取?
A class in my ASP.NET website needs to access a database table multiple times for every request. The database table should rarely change, if ever. Maybe a couple times a month.
In the static constructor of the class, the table is fetched from the DB and cached in a static local variable. Whenever the class needs to access the table, then, it just uses the cached, static version.
My question concerns the lifespan of this cached, static version of the table.
I understand that it's fetched the first time the class is instantiated or a static method in the class is used. But how often does this occur on the web server? What if the table changes and we want to reset this static version of the table?
Basically, I'm wondering, is this table fetched once and then only refetched each time I restart IIS? What, with regard to the site and IIS, will trigger this static class to reset, causing the static table to be refetched?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不将其添加到“应用程序”集合中,而不是类上的静态变量?它的生命周期很好理解(网站的生命周期),并且可以通过触摸 web.config 轻松回收。将其填充到 global.asax 的 Application_Start 方法中。
Rather than a static variable on a class, why not make add this to the 'Application' collection? It's lifetime is well understood (the life of the website) and can easily be recycled by touching web.config. Populate it in your Application_Start method of global.asax.
我建议使用 ASP.NET 缓存本身,而不是为每个特定的缓存项使用变量(现在只有一个表,但我确信还有增长的空间);通过这种方式,您可以指定过期时间以及其他内容,例如依赖项。
您可以在此处,更具体地说,在此处使用缓存。
要回答有关局部变量的生命周期或预期的问题,请参阅此链接,它应该比我更好地解释内部结构。
I'd recommend using the ASP.NET cache itself, rather than having variables for each particular cache item (a single table right now, but I'm sure there's room for growth); this way you can specify expiration, among other things, such as dependencies.
You can get information on the cache here, and more specifically, using the cache here.
To answer your question about the life-cycle, or expectancy of a local variable, see this link, which should do a better job of explaining the innards than I.
是的,你已经找到了那个位置。本质上,重新启动 IIS 将导致您的静态变量被“刷新”。如果您使用静态变量来存储这种东西(这可能不是最好的解决方案,但我试图直接回答您的问题而不会偏离主题),我建议您在数据层中构建一些代码,以便您的每次写入相关数据库表时都会更新静态变量。这意味着您不需要每次更新时都退回服务器。
还值得记住的是,静态变量在所有客户端请求之间共享,这通常会导致一些不可预测的多线程错误。
Yes, you've got that spot on. Essentially, restarting IIS will cause your static variable to be "refreshed". If you use a static variable to store this kind of thing (which may not be the best solution, but I'm trying to directly answer your question without getting sidetracked), I would advise you build some code into your data layer so that your static variable is updated each time the database table in question is written to. This will mean that you dont need to bounce the server each time you update it.
Its also worth remembering that static variables are shared across all client requests, this can often lead to some unpredictable multi-threading errors.