什么时候应该使用范围锁定(应用程序,服务器等...)与ColdFusion中的命名锁定? [英] When should I use Scope Locking (Application, Server, etc...) vs named locking in ColdFusion?

查看:144
本文介绍了什么时候应该使用范围锁定(应用程序,服务器等...)与ColdFusion中的命名锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时适合使用< cflock scope =application>或者它是ilk,而不是< cflock name =foo> ;?

When is it appropriate to use <cflock scope="application"> or it's ilk as opposed to <cflock name="foo">?

具体来说,我有兴趣使用CFLock保护应用程序,会话或服务器范围内的共享对象,但我也想了解不同的用途

Specifically, I'm interested in using CFLock to protect shared objects in the application, session, or server scopes, but I'm also interested in finding out about different uses of locking in ColdFusion.

推荐答案

在读取和写入可以在应用程序范围内变化的内容时应该使用。例如:

You should use when reading and writing from things that can change in the application scope. For example:

<cfquery name="application.myData">
    select * from myTable
</cfquery>

您将要锁定type =exclusive。在使用application.myData的地方,你需要一个type =readonly锁。例外是Application.cfc的OnApplicationStart方法,它会锁定自身。同样,对会话和服务器范围使用相同的策略。

You are going to want to lock that with type="exclusive". Wherever application.myData is used, you need a type="readonly" lock. The exception is Application.cfc's OnApplicationStart method, which locks itself. Likewise use the same strategy with the session and server scopes.

命名锁可以让您更好地控制锁定策略。当需要动态锁定命令时,请使用名为cflock。例如:

Named locks give you more control over your locking strategy. Use a named cflock when you need to lock commands dynamically. For example:

<cflock name="write_file_#session.user_type#" type="exclusive">
    <cffile action="write" name="file_#session.user_type#" output="#content#" />
</cflock>

在此示例中,允许不同类型的用户同时写入文件,但用户与相同的 session.user_type 必须等待对方。此cflock有助于避免文件争用问题。

In this example, users of different types are allowed to write a file at the same time, but users with the same session.user_type must wait for each other. This cflock helps avoid file contention issues.

使用命名锁的另一个原因是,如果您不知道当前操作的范围。如果你在一个实例化的cfc中,你怎么知道你被实例化的范围?变量?会话?应用?良好的封装教导我们,对象不知道什么,除了他们被告知。在CFC中,使用命名锁并在CFC后面命名,或根据您的用例使用CFC和唯一的实例变量。

Another reason to use a named lock is if you do not know the scope of your current operation. If you are in an instantiated cfc, how do you know what scope you were instantiated into? Variables? Session? Application? Good encapsulation teaches us that objects do not know anything except what they have been told. Inside a CFC, use a named lock and name it after the CFC, or the CFC and a unique instance variable depending on your use case.

这篇关于什么时候应该使用范围锁定(应用程序,服务器等...)与ColdFusion中的命名锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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