• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Apr 29, 2014 Apr 29, 2014

Copy link to clipboard

Copied

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

Views

579

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 29, 2014 Apr 29, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 29, 2014 Apr 29, 2014

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation