Skip to main content
May 7, 2010
Question

CFC caching

  • May 7, 2010
  • 2 replies
  • 1207 views

Hi,

So in our application, we cache CFC objects in application scope so that when we use the objects we avoid having to instantiate them again which presumably take extra time.  However, I have been wondering whether it's true that further instantiations do take extra time.  Let me clarify: suppose I have a set of CFCs that I just wrote and I create a script to instantiate them for the first time.  That process will take some noticeable time.  However, the subsequent times I run the same script there is almost no processing time.  It looks to me that ColdFusion is caching these objects already.  And as long as I do not change any of the CFCs, new instantiations happen without any noticeable time.

Is that true that CF caches these instantiated CFC objects automatically?  In which case there is no difference in processing time between using application-scope CFC object and instantiating whenever we need to use the object.  Btw, I realize that there may still be differences in terms of amount of memory used.

This question has been bothering me a bit, so I'd appreciate if someone could clarify it for me.  Thank you!

Sincerely,

Min

    This topic has been closed for replies.

    2 replies

    Inspiring
    May 9, 2010

    CF will also cache the last n templates (CFM files, CFC methods) in memory after they've been called the first time.  n is a setting in CF Administrator (I cannot remember which screen... either Settings or Cache, probably).

    However caching the class used to instantiate the object, and caching an object instance is two different things.  There is still a real-world performance consideration when instantiating CFCs (even in CF9 in which it's pretty bloody quick), but it's something measured in tens-of-milliseconds, so it's not something a human will notice.  However if you're using lots of CFC instances and want to keep your CF processing time down to a coupla hundred ms, instantiating a CFC is a reasonable chunk of that.

    --

    Adam

    May 7, 2010

    cfide>caching>

    Is Save Class Files ticked or no?

    If yes, then experiment with it off, you should find that the start up time is the same.

    May 7, 2010

    Ah I see.  Didn't notice that setting before.  Sorry for my ignorance, what are considered class files?

    So, if the option to cache CFC is selected, would it be correct to say that the only advantage of instantiating CFC in application scope is for better memory management?

    Thanks!

    Min

    ilssac
    Inspiring
    May 7, 2010

    There are two levels of CFML code processing that goes into what you are trying to measure.

    First, all CFML code, including CFC, must be compiled into JavaByte Code by ColdFusion.  This is what those class files are, the JavaByte code.  ColdFusion can be configured to cache these to a file directory or not, to test every request whether the class needs to be recompiled or not.  These options have various levels of performance impacts.  For example, if you configure ColdFusion to not check for new source code with every request, you must manually clear the class files if you want new code to get compiled.  Also, saving the class files can sometimes be slower if their are so many that a file system directory search to find the class file takes longer the compiling the files would.  So it can take some trial and error and testing to figure out what is best for your system.

    But this compilation of new CFML code is by far the majority of the time you are noticing when you first run code.  But there is a smaller fraction of that time that is the result of instantiating a CFC object into memory.  This is considerable less and will depend on the size on complexity of the object.