frequent
frequent

Reputation: 28493

Should heavy-use objects be created on application/session level in Coldfusion?

I'm running Coldfusion8/MySQL 5.0.88.

My applications main feature is a search function, which on submit triggers an AJAX request calling a cfc-method. The method assembles the HTML, gzips it and returns gzipped HTML as Ajax response.

This is the gzip part:

 <cfscript>
 var result="";
 var text=createObject("java","java.lang.String").init(arguments[1]);
 var dataStream=createObject("java","java.io.ByteArrayOutputStream").init();
 var compressDataStream=createObject("java","java.util.zip.GZIPOutputStream").init(dataStream);
 compressDataStream.write(text.getBytes());
 compressDataStream.finish();
 compressDataStream.close();
 </cfscript>

I am a little reluctant regarding the use of cfobject here, especially since this script will be called over and over again by every user.

Question:
Would it increase performance if I create the object on the application or session level or at least check for the existence of the object before re-creating it. What's the best way to handle this?

Upvotes: 1

Views: 192

Answers (1)

barnyr
barnyr

Reputation: 5678

If your use of objects is like what's in the code snippet in the question, I'd not put anything into any scope longer-lived than request. The reasons being:

  • The objects you are instantiating are not re-usable (Strings are immutable, and the output streams don't look re-usable either)

  • Even if they were re-usable, the objects in question aren't thread-safe. They can't be shared between concurrent requests, so application scope isn't appropriate and actually session scope probably isn't safe either as concurrent requests for the same session can easily occur.

  • The objects you're using there are probably very low overhead to create, so there'd be little benefit to trying to cache them, if you could.

If you have objects that are really resource intensive, then caching and pooling them can make sense (e.g. Database Connections), but it's considerable effort to get right, so you need to be sure that you need it first.

Upvotes: 5

Related Questions