ASP.Net静态对象 [英] ASP.Net static objects

查看:217
本文介绍了ASP.Net静态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试缓存从数据库检索到的一些信息。我决定使用一个静态的List<>成员来存储信息。我从我在多线程应用程序中的List<>的经验中知道,我需要使用lock语句来保护对它的访问。我是否以相同的方式处理我的Asp.Net代码中的任何代码?锁定语句仍然可以工作吗?

I'm trying to cache some information that I've retrieved from a database. I've decided to use a static List<> member to store the information. I know from my experience with List<> in multithreaded applications that I need to protect access to it with the lock statement. Do I treat any code in my Asp.Net code the exact same way? Will the lock statement still work?

推荐答案

锁定将工作。请注意,如果有多个工作进程,则最终会有多个版本的列表。每个人都将受到自己的锁保护。

Lock will work. Be aware that if there are multiple worker processes, you will end up with multiple versions of the list. Each of them will be protected by its own lock.

一个相当真正的危险是,如果你无法释放锁,你的整个web应用程序可能会挂起。在执行ASP.NET页面时,用户可能会断开连接;因此请注意在意外时间抛出的异常。

A pretty real danger here is that if you fail to release the lock, your entire web application might hang. A user might get disconnected while your ASP.NET page is executing; so be careful of exceptions thrown at unexpected times.

这篇关于ASP.Net静态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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