Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shortening script 1

Status
Not open for further replies.

oaklandar

Technical User
Feb 12, 2004
246
US
Anyway to put this in a loop or shorten what I am doing?

Code:
<cflock name="clock" type="Readonly" Timeout="30">
<cfdirectory action="LIST" directory="/directory/dirThree" name="myQ1" filter="*.*">
</cflock>

<cflock name="clock" type="Readonly" Timeout="30">
<cfdirectory action="LIST" directory="/directory/dirTwo" name="myQ2" filter="*.*">
</cflock>

<cflock name="clock" type="Readonly" Timeout="30">
<cfdirectory action="LIST" directory="/directory/dirThree" name="myQ3" filter="*.*">
</cflock>

<cfoutput>#myQ1.recordcount# Files</cfoutput>
<cfoutput>#myQ2.recordcount# Files</cfoutput>
<cfoutput>#myQ3.recordcount# Files</cfoutput>
 
You could do it in less lines of code, but it wouldn't run any faster.

But..

CFLOCK doesn't really need to be used here.. In the years I've used Cf, I only got one file read error. I'm gonna rewrite your code.

Code:
[red]<cflock name="clock" type="Readonly" Timeout="30">[/red]
  <cfdirectory action="LIST" directory="/directory/dirThree"
    name="myQ1" filter="*.*">
  <cfdirectory action="LIST" directory="/directory/dirTwo" 
    name="myQ2" filter="*.*">
  <cfdirectory action="LIST" directory="/directory/dirThree"
    name="myQ3" filter="*.*">
[red]</cflock>[/red]

<cfoutput>#myQ1.recordcount# Files</cfoutput>
<cfoutput>#myQ2.recordcount# Files</cfoutput>
<cfoutput>#myQ3.recordcount# Files</cfoutput>

I'm sure you can remove the red CFLOCKs but you may leave them if you like.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
You seem to answer alot of my questions on here and I appreciate your time and helping me with my Cold Fusion!!
 
Its not a problem.

Your code and my revisions did exactly the same thing as far as logic and the locking process.. but here's a tip.

You used a seperate CFLOCK for each dir read.. and that's fine, you can do that. The thing is you named all the locks the same.. so you'd get erratic performance.. If two users hit simultaneously (and one user right after that) you'd get performance something like this.

The numbers to the side represent order of execution and the numbers at the top represent individual users.

Code:
  | User 1 | User 2 | User 3
1 +        | lock 1 | 
2 + lock 1 |        | 
3 +        | lock 2 | 
4 +        |        | lock 1
5 + lock 2 |        | 
6 +        | lock 3 | 
7 +        |        | lock 2
8 + lock 3 |        | 
9 +        |        | lock 3

Which would increase the delay for everyone.. its the same amount of code but it takes longer because in the example above.. User 2 would have to wait to User 1 was almost done before their request finished. User 1 would have to wait til after User 2 was done and User 3 was almost done.

Also, you cflock takes a bit to open.. its gotta tell a core property of cf to change.. and while it does it fast once.. Under a load it may not be so fast.

You want to use cflock as little as possible and the same goes for cfoutput and # as well.

One article I read said that if you consider that CF is a car and each cfoutput and /cfoutput represents starting and stopping a car.. Imagine what that does to your engine.

# is the same way.. You'll see things like these a lot.

Code:
<cfif #Var# is "">

<cfset mytime=#Now()#>

All you need to do for both of those is..

Code:
<cfif Var is "">

<cfset mytime=Now()>
because # is like the start/stop car scenario on a much smaller scale.

Just trying to get you in good habit now. And you may use some of these methods already, or you may not.. This just sort of rolled out this morning.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Thanks again!

I changed it just as you suggested and it now looks like this:
Code:
<cflock name="clock" type="Readonly" Timeout="30">
  <cfdirectory action="LIST" directory="/directory/dirThree"
    name="myQ1" filter="*.*">
  <cfdirectory action="LIST" directory="/directory/dirTwo" 
    name="myQ2" filter="*.*">
  <cfdirectory action="LIST" directory="/directory/dirThree"
    name="myQ3" filter="*.*">
</cflock>

<cfoutput>#myQ1.recordcount# Files</cfoutput>
<cfoutput>#myQ2.recordcount# Files</cfoutput>
<cfoutput>#myQ3.recordcount# Files</cfoutput>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top