何时在应用程序范围内使用锁定

发布于 2024-07-08 11:11:58 字数 151 浏览 7 评论 0原文

我想知道是否需要锁定在应用程序范围内创建的一些代码。 如果我在应用程序范围内创建一个对象,例如 userDAO.cfc,那么它可用于所有页面。 那么,如果我在该对象中有一个方法(例如 getUserInfo(userID)),该方法将在应用程序的不同部分中调用,我是否需要锁定此方法?

I am wondering if I need to lock some code created in the application scope. If I create an object say userDAO.cfc in application scope so its available to all pages. Then if I have a method in that object say getUserInfo(userID) that would be called in different parts of the applications, do I need to lock this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2024-07-15 11:11:58

简短的回答:可能不会。

如果该对象是在 Application.cfc 的 OnApplicationStart() 方法中在应用程序范围内创建的,并且它永远不会更改,并且您确保对所有函数的所有变量进行 var 更改,那么您不需要锁定访问到它。

在这种情况下,OnApplicationStart() 会为您执行锁定,并且在创建该方法之前不会让任何人继续操作。 这很好,因为它只允许该项目被创建一次,并在其他人使用它之前确保它存在。

如果您从其他任何地方实例化该对象,例如某个随机页面的顶部,那么是的,您将需要在此处以及引用它的任何地方锁定它。

如果该对象将更改为另一个对象,或者它保留任何状态,例如更改为其他内容的私有或公共变量(variables.* 或 this.*),您可能需要从外部锁定它,或者您可以尝试锁定它基于每个变量位于 CFC 内。

最后,如果您不 var 所有变量(使用 Mike Schierberl 的 varScoper!),那么您将改变对象的状态。 你永远不应该真正走到这一步,但这是锁定的情况。

整个事情的一个大例外是,如果您使用 CF 5 或更低版本,在这种情况下,任何共享范围访问都必须被锁定,如果您使用 CF 6 或 6.1,在这种情况下,没有 Application.cfc。

Short answer: probably not.

If that object is created in the application scope from within your Application.cfc's OnApplicationStart() method, and it never changes, and also you make sure to var all of your variables for all your functions, then you won't need to lock access to it.

In this case, OnApplicationStart() does the locking for you, and won't let anyone proceed until that method is created. This is good because it will only allow the item to be created one time and will make sure it exists before anyone else uses it.

If you instantiate the object from anywhere else, like the top of some random page, then yes, you will need to lock it here, and everywhere that references it.

If the object will ever change into another object, or if it keeps any state, like private or public variables (variables.* or this.*) that change into other things, you may want to lock it externally, or you could try locking it inside the cfc on a per-variable basis.

Finally, if you don't var all your variables (use Mike Schierberl's varScoper!), then you will be changing the object's state. You should never really come to this point, but it is a case for locking.

Big exceptions for the whole thing are if you are using CF 5 or below, in which case any shared scope access MUST be locked, and if you are using CF 6 or 6.1, in which case there is no Application.cfc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文