Jon Hall wrote:
> I have seen all of the warnings to make sure to use <cflock> around any of
> the in memory variable types in a CF application. I hear that every access
> of a session, or application variable should have <cflock> around it, or I
> could enable automatic locking on the server which will exact a performance
> hit though. Is there a different kind of lock I should use when reading a
> session or app variable than when I am writing to one of these variables?
As long as you are using cf 4.5, make sure you use the scope attribute of a CFLOCK.
<CFLOCK scope="session" (for session variables)
<CFLOCK scope="application" (for application variables)
> How about when I create a session or app variable?
You want to use the type attribute of CFLOCK.
<CFLOCK type="exclusive" (for reading)
<CFLOCK type="read-only" (for writing)
At this point, I hope I haven't swapped the use of type and the use of scope, so
check your documentation. ;)
> Here is a bit of code from a shoppingcart that I have written. It's is the
> addtocart()... Where should the cflocks go? Since I am not totally sure
> about cflock and it's uses I currently just cflock all of this tag,
> essentially single threading the entire function. This probably isn't too
> bad, since performance isn't a big requirement for the sites that I am using
> this code on. In my never ending quest for that extra millisecond though,
> understanding cflock is the next step.
>
> <cfif isDefined("attributes.price") and isDefined("attributes.quantity")
> and isDefined("attributes.itemcode")>
>
> <cfif NOT isDefined("session.cart")>
> <cfset session.cart = structnew()>
> </cfif>
>
You'll want to use an exclusive lock here.
>
> <cfset temp = ListToArray('#attributes.price#,#attributes.quantity#')>
>
> <cfif NOT structKeyExists(session.cart, attributes.itemcode)>
> <cfset rs = structInsert(session.cart, attributes.itemcode, temp)>
> <cfelse>
> <cfset temp[2] = attributes.quantity>
> <cfset rs = structUpdate(session.cart, attributes.itemcode, temp)>
> </cfif>
>
You'll want to put a lock around the above if statement, also exclusive.
>
> </cfif>
>
Why not remove all session variable access from your 'function'? Create a temporary
variable for your cart and
pass it back into the caller (if this is a custom tag). You eliminate the need for
CFLOCKs within your function.
After the function is complete, just add a line (surrounded by CFLOCK) <CFSET
session.cart = mytempcart>
Food for thought.
--
Jeff Houser | mailto:[EMAIL PROTECTED]
AIM: Reboog711 | ICQ: 5246969 | Phone: 860-229-2781
--
Instant ColdFusion 4.5 | ISBN: 0-07-213238-8
Due out 3rd Quarter 2001
--
DotComIt, LLC
database driven web data using ColdFusion, Lotus Notes/Domino
--
Half of the Alternative Folk Duo called Far Cry Fly
http://www.farcryfly.com | http://www.mp3.com/FarCryFly
--
I've got the brains, you've got the looks, let's make lots of money
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at
http://www.fusionauthority.com/bkinfo.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists