loopscoldfusioncfinvoke

Should I use cfobject or cfinvoke for a repetitive task?


I need to loop over a set of image paths to grab, resize and store images from external destinations to S3.

I'm used to calling cfcs to do this like so:

<cfinvoke component="form_img_handler" method="upload" returnvariable="imgSuccess">
    <cfinvokeargument name="command" value="upload_search"/>
    <cfinvokeargument name="imgPath" value="#results.bildpfad #"/>
    <cfinvokeargument name="imgFile" value="#results.bilddateiname#"/>
    <cfinvokeargument name="sellerILN" value="#results.iln#"/>
    <cfinvokeargument name="cookie" value="#variables.screenWidth#"/>
</cfinvoke>

Question:
If I have to do this 25x, in a loop, would it be better to use cfobject instead of cfinvoke? From what I understand cfinvoke gets instantiated, runs its job and perishes. While cfobjects are there to stay. If so, would it better in the above case to use cfobject? If so, how would I call my upload function (passing parameters) and how do I remove the object once I'm done?

(never used cfobject before...)

Thanks for help!


Solution

  • Neither, use something like...

    Outside loop (possibly in global scope, e.g. Application):

    <cfset form_img_hander = createObject('component','dotted.path.to.form_img_hander') />
    or
    <cfset form_img_hander = new dotted.path.to.form_img_hander() />
    

    Inside loop:

    <cfset imgSuccess = form_img_handler.upload
        ( command   = "upload_search"
        , imgPath   = results.bildpfad
        , imgFile   = results.bilddateiname
        , sellerILN = results.iln
        , cookie    = variables.screenWidth
        )/>
    

    Because it's far more readable.


    You don't have a performance problem, unless you have a repeatable test-case which proves that you have a performance problem.


    Regarding removing objects...
    If you're not placing objects in a persistent scope, you don't need to worry about removing them - they are only tied to the request and once the request has ended they will be garbage collected as required.

    If you are placing objects in a persistent scope, you probably still don't need to worry about removing them, but if you determine that you do, you can use StructDelete to remove it (just like any other variable). Of course, you should be careful not to do that whilst it's needed.