View Single Post
  #16  
Old 02-18-2008, 09:04 PM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,264
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up ColdFusion Tips & Tricks - Duplicating the Session scope

Duplicating the Session scope


If you find a program or custom tag that places an exclusive lock around the session scope and then performs some timely processes, here's a means you can use to release the bottleneck that can pretty much freeze up the server under even a light load.

First thing you need to do is to duplicate the session scope in a local scope. You can use the Request scope or copy it to the variables scope - the choice is yours. However, in this example, I will use the variables scope because this process is occurring in a custom tag and I wish for the copy of the session scope to be removed from memory upon completion of the tag's execution.
Code:
<cflock timeout="30" throwontimeout="Yes" type="READONLY" scope="SESSION">
  <CFSET variables.LSess=Duplicate(session)>
</cflock>
Now it's simply a matter of replacing all references to "session." to "variables.LSess" in the template. You can be sure that you caught every session simply by searching for "session" (or running the template if you have scope locking enabled in the CF Admin and it'll blow chunks where you missed it).

Now comes the important step - copying the local copy of the session structure back to the actual session scope. Despite what you may think, the Duplicate function will not work for this.

For example:
Code:
<cflock timeout="30" throwontimeout="Yes" type="EXCLUSIVE" scope="SESSION">
  <CFSET session=Duplicate(variables.LSess)>
</cflock>
Now, mostly this does indeed work. However, for some reason, structures that are contained in your local copy do not copy over to the session scope. Really friggin' frustrating in trying to figure out why some of your application works fine but others do not, let me tell you.

However, there is another way to ensure that everything gets copied back over to the session scope though it's not as efficient as the original copy. You have to Duplicate() each key under the local session structure over to the session scope.

Code:
<cflock timeout="30" throwontimeout="Yes" type="EXCLUSIVE" scope="SESSION">
  <CFLOOP index="Key" list=#StructKeyList(variables.LSess)#>
    <CFSET "Session.#Key#"=Duplicate(Evaluate("variables.LSess.#Key#"))>
  </CFLOOP>
</cflock>
__________________
Prasanna Vignesh
MCPD | Web Developer
Reply With Quote