2 Replies Latest reply on Apr 29, 2014 9:36 AM by PhilBro

    What are the pros and cons of storing heavely used CFCs in the application scope?

    PhilBro Level 1

      I've been storing all the required CFCs for a site in the application scope. During onApplicationStart I do something like this application.objSomeCfc =CreateObject('component', 'com.someCfc').init().

       

      Here is my reasoning.

      • Get the CFCs initialized once and stored in memory for better performance.
      • Using CreateObject several times on each page load can have a negative impact on performance.
      • Having one place to create application scoped CFCs makes it easier to manage code.

       

      So is my thinking flawed? Are there any additional pros or cons for dealing with CFCs? Is there any docs, articles, blogs, videos, frameworks, ...... that I should check that may change my perspective on how I'm doing this?

       

      Thanks

        • 1. Re: What are the pros and cons of storing heavely used CFCs in the application scope?
          Carl Von Stetten Adobe Community Professional & MVP

          Your approach is fine, depending on how many and what type of CFCs you are talking about. If they are "singletons" - that is, only one instance of each CFC is needed to be in memory and can be reused/shared from multiple parts of your application - caching them in the application scope is common.  Just make sure they are thread safe ("var" or local.* all your method variables).

           

          You might consider taking advantage of a dependency injection framework, such as DI/1 (part of the FW/1 MVC framework), ColdSpring, or WireBox (a module of the ColdBox platform that can be used independently).  They have mechanisms for handling and caching singletons.  Then you wouldn't have to go to the application scope to get your CFC instances.

           

          -Carl V.

          • 2. Re: What are the pros and cons of storing heavely used CFCs in the application scope?
            PhilBro Level 1

            They are all singletons and for the most part thread safe. I still find some old code here and there that  isn't but I fix it as soon as I find it. I was curious because I was told it was "bad". I've been doing it for years with out issue and never heard heard anyone say it was bad until recently.

             

            Thanks for the info on the frameworks. I've been trying to think of a way to do what DI/1 already does. Good to know there is somethign already out there.