1

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?

frequent
  • 27,643
  • 59
  • 181
  • 333
  • I would try it and see what difference it makes. – Peter Lawrey Sep 03 '12 at 10:33
  • I only say it in case no one has a good idea of what you should do .... ;) – Peter Lawrey Sep 03 '12 at 10:40
  • 1
    I'm just curious how the object would run on application/session scope if it's constantly being called (especially by different users). If every request "has it's object"... I don't need to worry about this, but this can be a lot of objects. Since I'm not that familiar with objects, we will see what other answers I'm getting. Thx – frequent Sep 03 '12 at 10:43
  • 2
    Dumb question - but doesn't Apache support gzipping responses automatically? Why not just return the HTML and let Apache (or IIS) gzip it before sending it back? – Raymond Camden Sep 03 '12 at 12:47
  • I have gzip enabled and while it I'm getting gzipped files, my Ajax responses to update page elements were not gzipped. So I'm using [this](http://cflib.org/udf/gzip#examples) to gzip manually. – frequent Sep 03 '12 at 13:20
  • @RaymondCamden: question would be if something I'm assembling server-side (like 25 HTML list items) and passing it back to the page through Ajax is considered a "gzippable" file? The Apache docs always speak of files (implying static files?). – frequent Sep 03 '12 at 13:30
  • 1
    You can gzip an ajax response quite happily, we do it through a compression servlet added to ColdFusion's web.xml. Check out the Question here for suggestions: http://stackoverflow.com/questions/4755302/which-compression-is-gzip-the-most-popular-servlet-filter-would-you-suggest – barnyr Sep 04 '12 at 08:06
  • @barnyr: cool. thanks for the link. My own solutions is working fine as well, except for myself being unsure whether to declare objects globally or every time my script runs. – frequent Sep 04 '12 at 08:13

1 Answers1

5

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.

barnyr
  • 5,678
  • 21
  • 28
  • ok. thanks for clarifying. I was thinking somewhere along these lines without really knowing if this was correct or not. Pretty sure this is nothing resource intensive, so I will leave it like it is. THANKS! – frequent Sep 04 '12 at 08:56