您是否在Web应用程序中使用锁定? [英] Do you use lock in your web applications?

查看:62
本文介绍了您是否在Web应用程序中使用锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布此问题后:

如何在C#中锁定整数?

许多答案让我觉得我是在Web应用程序中使用锁定的罪人".我从来没有想过这是一个问题(如果使用得当的话),那你呢?您是否曾在Web应用程序中使用过它?

Many of the answers made me feel that I'm a 'sinner' for using lock in my web apps. I never thought this is a problem (if used wisely), what about you? Do you ever use it in your web applications?

我不知道如何在不锁定的情况下编写Web应用程序,例如,如果您想从数据库中加载一些数据,并且希望确保没有其他线程可以加载它(对于单例来说),通常您使用锁定,例如:

I don't see how a web application can be written without locking, for example if you want to load some data from a database and you want to be sure that no other thread will load it (also for singletons), normally you use locking, for example:

private _locker =  new object();
private YourClass[] _data;
public YourClass[] Data
{
    get
    {
        if(_data == null)
        {
            lock( _locker)
            {
                // get your data
               _data = GetYourData();
            }
        }
        return _data;
    }
}

这有问题吗?!

请注意,我在这里指的是单个服务器方案,对于服务器场,您需要某种分布式锁定机制,但是您不希望自己创建的每个网站都在几周内获得数百万次点击你?如果您需要锁定,应该使用分布式锁定来创建站点,对于您不知道是否需要扩展的应用程序来说,这不是太多吗?除了这几天的计算机发展非常快之外,一台服务器可以处理大量流量,并且已经被证明了很多次,一些例子是muchoffish.com,您现在正在使用的这个站点上,可以进行谷歌搜索,我敢肯定您会遇到很多其他人.

Please note that I'm referring to a single server scenario here, for a server farm you need some distributed locking mechanism, but you don't expect every site you create to get millions of hits in a couple of weeks, do you? What if you need locking, should you create your site with that distributed locking, isn't that too much for an application which you have no idea whether it will ever need to be scaled or not? Besides computers have gotten really fast these days, one server can handle tons of traffic and this has been proven so many times, some examples are plentyoffish.com and this very site you're using right now, do some googling and I'm sure you'll come across so many others.

推荐答案

如果您使用锁来尝试控制对远程资源(例如数据库中的内容)的访问,那么您将拥有尝试扩展到更多服务器时出现问题.

If you use a lock to try to control access to a remote resource (e.g. something from the database) then you'll have problems when you try to scale to more servers.

如果您只想拥有特定于 AppDomain 的锁(例如,用于缓存),那么这是很合理的.

If you just want to have an AppDomain-specific lock (e.g. for a cache) then it makes perfect sense.

我不同意您的暗含断言,即Web应用程序必须在未锁定的情况下(在Web服务器级别)才能编写.它实际上取决于应用程序-通常,数据库争用是在数据库而不是在Web服务器上处理的,正是这样,您才可以在Web层中拥有多台计算机.

I disagree with your implied assertion that web applications can't be written without locking (at the web server level) though. It really depends on the application - usually database contention is handled at the database rather than the web server, precisely so that you can have multiple machines in your web tier.

对于单身人士来说,这是有一定意义的,但是我还是很少使用单身人士...

For singletons it makes a certain amount of sense, but I rarely use singletons anyway...

这篇关于您是否在Web应用程序中使用锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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