Welcome!

ColdFusion Authors: Greg Ness, Liz McMillan, Pat Romanski, Andreas Grabner, David Strom

Related Topics: ColdFusion

ColdFusion: Article

Ask the Training Staff

Ask the Training Staff

Wow! Can you believe it's June already? The last few months have been blurred by the flurry of excitement around the new MX products announced and released by Macromedia.

As you'll see from the first question, I'm very excited about the new CFMX product. I know you'll like it once you give it a spin. I'd like to hear your comments and questions about it.

Q: I've been hearing a lot of talk about the new CFMX. Have you had a chance to play with it? If so, what do you like about it? And do you recommend upgrading?
A: There has been a tremendous amount of talk about CFMX and all the new MX products from Macromedia. At the time of this writing the preview editions are available for downloading - they're very exciting. I've been participating in the beta program and have gotten to test-drive most of the new features. Bottom line: I love it! If you don't plan to use any of the new code features, you should still upgrade. Your existing code will run much faster than it does on ColdFusion 5.0. I ran a simple test on a server running both CF5 and CFMX. The same code took an average of 321ms to run on CF5 but only 62ms on CFMX. Definitely get a copy and try the new features and speed for yourself.

Q: Is there a limit to the number of scheduled tasks we can run on a CF server? Currently we have nearly 20 different scheduled tasks and are trying to figure out whether we should get a separate server dedicated only to our scheduled tasks.
A: No, there's no limit. CF uses very little overhead to check whether a scheduled task should be executed. The scheduler simply initiates an HTTP request for the specified page, just as a real user would. If your system can handle 20 simultaneous users, it can handle 20 scheduled tasks. Your application server, however, may feel the strain if all of your tasks are scheduled to execute at the same time. To spread out the server load, try to avoid having all of your tasks execute at the same time.

Q: I'm trying to build an application that emulates the way a human interacts with a page, and I need to create pauses between page selections. How can I make CF wait 10 seconds before processing the next section of code?
A: There's a right way and a wrong way to do this. The easiest thing is to create a conditional loop that simply loops until 10 seconds has passed (see listing below for an example). This isn't the best method, however, because it's very resource intensive. Essentially, every time this code runs, it will use 100% of your CPU resources. If the wait isn't long and there aren't many users on your server, this may be okay, but I don't recommend it. For the best solution go to Macromedia's Developer Exchange (http://devex.macromedia.com/developer/gallery/) and download either CFX_Sleep or CFX_Wait. Both of these custom tags do the same thing and use very few system resources.

<!--- set a variable equal to the now() function --->
<cfset StartWait = Now()>
<!--- create a conditional loop that evaluates the
difference in seconds between now() and the value
set in the StartWait variable--->
<cfloop condition="DateDiff('s', Variables.StartWait, now()) lt 10"/>

Q: I have a problem with clearing session variables. I use the automatically generated Session.URLToken variable on all my URLs so I don't have to use cookies for session management. When a user logs out, I clear the session scope using StructClear(Session). This, however, deletes CFID, CFTOKEN, and URLTOKEN. Is there an easier way to clear out a user's session variables without deleting these built-in variables?
A: Yes, you have three options here. First, you could create a new structure in the session scope, like Session. UserData (see listing below), and add all of the variables you were storing directly in the session scope to this new structure. If you're using a variable like Session.LoggedIn to check a user's login status, you'd simply change your code to check for Session.UserData.LoggedIn. When your user logs out, simply delete the UserData structure from the session scope using StructDelete(Session, "UserData"). This erases the data associated with that user without deleting the other session variables the CF created automatically. This is probably more code than you want to write, however, since you have to change every reference from Session.LoggedIn to Session.UserData.LoggedIn.

<!--- Where you normally initialize your session
variables, create a new container called UserData --->
<cfset Session.UserData = StructNew()>

<!--- Then add your normal
session vars to this new UserData container --->
<cfset Session.UserData.LoggedIn = 1>

<!--- At logout, delete the UserData container --->
<cfset tmp = StructDelete(Session,"UserData")>

Perhaps a better way is to simply change the code you run at logout. Instead of clearing out the entire session scope using StructClear-(Session) - which is certainly short and easy - you could loop over your session variables to delete all but the ones CF created (see listing below). While this is more code than you currently have in your logout process, it's less than you need to modify all your session variable references as outlined above.

<!--- Code to run when user logs out of application --->
<!--- Create a list of CF-Generated Session Variables --->
<cfset CF_Variables = "CFID,CFTOKEN,SESSIONID,URLTOKEN">

<!--- Loop over the Session scope --->
<cfloop collection="#Session#" item="VarName">
<!--- if the session variable is NOT
in the CF_Variables list, delete it --->
<cfif not ListFindNoCase(CF_Variables, VarName)>
<cfset tmp = StructDelete(Session,VarName)>
</cfif>
</cfloop>

For my money, however, there's an easier method. If your login process always reinitializes the session variables for each user, it's usually sufficient to delete only the one variable you're using for login validation. For example, if you check for Session.LoggedIn in your application pages, all you have to do is delete that one variable. Your other session variables will be overwritten or erased when the user logs in again, and Session.URL Token will remain intact. Instead of StructClear(Session), simply use StructDelete(Session,"LoggedIn").

*  *  *

Please send your questions about ColdFusion (CFML, CF Server, or CF Studio) to AskCFDJ@sys-con.com. And please visit our archive site at www.NetsiteDynamics.com/AskCFDJ.

More Stories By Bruce Van Horn

Bruce Van Horn is president of Netsite Dynamics, LLC, a certified ColdFusion developer/instructor, and a member of the CFDJ International Advisory Board.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
R. Rajan 12/04/03 09:47:20 AM EST

I would like to capture the execution time of each page and store it in the database for future analysis. Using getTickCount() in every page at this stage is lot of work. Is there any way I can capture the execution time displayed at the bottom of the page