GetMetaData() Is Shared By All Instances Of A Given Class In ColdFusion

Posted July 21, 2009 at 1:53 PM by Ben Nadel

Tags: ColdFusion

This is just a minor note, but I found out the other day that the structure returned from ColdFusion's GetMetaData() method is not unique to the instance on which it was called. Rather, the structure seems to be shared among all instances of a given class (component definition). This means that if you update the Meta Data for a given CFC instance, it updates the Meta Data for all instances of the same time. And what's more, this has nothing to do with the persistence of any of the objects in question. Even if you do this on a page where the object is not cached, newly created instances of the given component on subsequent page requests will still have the updated meta data. And, on top of that, the Meta Data does not seem to be related to the application timeout. I guess the meta data for a given class gets stored in the trusted cache or something?

To see this in action, take a look at the following code:

  • <!--- Create a test component. --->
  • <cfset firstTest = createObject( "component", "Test" ) />
  •  
  • <!--- Create another test component. --->
  • <cfset secondTest = createObject( "component", "Test" ) />
  •  
  •  
  • <!---
  • Now that we have two instances of our test component,
  • update the meta data for just one of them.
  • --->
  • <cfset getMetaData( firstTest ).forBlog = true />
  •  
  •  
  • <!---
  • Output the meta data for both components to see how
  • they were updated.
  • --->
  • <cfdump
  • var="#getMetaData( firstTest )#"
  • label="First Test Object"
  • show="name,forblog"
  • />
  •  
  • <br />
  •  
  • <cfdump
  • var="#getMetaData( secondTest )#"
  • label="Second Test Object"
  • show="name,forblog"
  • />

The actual component here is empty, which is irrelevant. What's important to notice is that I am creating two separate instances of the component, but only changing the meta data on one of those instances. When they are then both CFDump'd out to the page, we get the following output:

 
 
 
 
 
 
GetMetaData() Structure Is Shared Among All Instances Of A Given Class In ColdFusion. 
 
 
 

As you can see, the meta data for both of the ColdFusion component instances now contains the "forBlog" key even though only one of the instance' meta data was directly updated.

This is a really minor piece of information; but, I recently started playing around with meta data and this minor point became an important caveat to understand. Perhaps more on this later.



Reader Comments

Jul 21, 2009 at 10:26 PM // reply »
2 Comments

woow ben, you discovered the Singleton design pattern....way to go buddy !


Jul 22, 2009 at 1:12 AM // reply »
26 Comments

yes yes yes!
I came across this exact same thing when working with my CFproperty Inspector code (cfproperty.riaforge.org) when working with sniffing for component properties.

This gave me some bad ideas about implementing (via coldspring) on-demand property manipulation at run time - which would essentially introspect the component, automatically update the properties of the component dynamically (based on your getter methods), and then once setup, be available to flex for Class binding etc.

I opted for writing these properties directly to the component instead, as cfproperty tags and updating them easily via Bolt's Extensions with a simple right click 'Inspect CFC' instead. (bolt extension also found on riaforge) This way you'd have more granular control over those properties that were 'meta data' in the component - but comes with some 'upkeep' in keeping this data aligned with your methods every time you add new getters.

Fun stuff -


Jul 22, 2009 at 7:54 AM // reply »
10,640 Comments

@Billy,

I wouldn't say that I discovered the singleton pattern; what I did was find out that meta data for a given class is a singleton, probably creating at first compile of the code, and then shared by all instances of the class. I also figured out that it was:

A) Not created via reflection for each instance.

B) Persistent.

Although, that makes me wonder - would changing the structure of the CFC at run time change it's meta data. You've given me some more stuff to experiment with!

@Kevin,

Yeah, writing directly to the CFC would probably be the best move. Oddly enough, I actually stumbled upon this with meta data about Function objects, not components, where there is no option to write directly.


Jul 22, 2009 at 8:53 PM // reply »
21 Comments

Interesting... It seems the meta data is being stored with the template proxy cache. I guess they figured it wouldn't change much and people wouldn't try to modify it...

The cache hangs around until you run:

createObject('component','CFIDE.AdminAPI.runtime' ).clearTrustedCache('/full/path/to/your.cfc');

Or click the Clear Template Cache Now button on the Caching page in your cf admin.


Jul 23, 2009 at 8:04 AM // reply »
10,640 Comments

@Mike,

Interesting. So, I guess the meta data won't change if the CFC is changed at runtime. It's worth looking into.


Aug 2, 2009 at 11:35 PM // reply »
14 Comments

I'm hoping for: THIS.enableAppTrustedCache & clearAppTrustedCache()


Aug 5, 2009 at 9:18 AM // reply »
10,640 Comments

@Aaron,

Clearing the trusted cache would be interesting. I am not 100% sure of all the things that go in the trusted cache. Are these basically the compiled templates and CFCs?


Aug 29, 2009 at 10:08 PM // reply »
14 Comments

Sry for the late reply. Yes, pretty much. "Template Cache" is the place in memory where compiled CFM and CFC templates are stored. When the "Trusted Cache" box is checked, in the Admin, CF doesn't inspect these requested templates for updates.

This is server-wide tho (w/ a "Clear Template Cache Now" button in the Admin). I'd like to leave Trusted Cache enabled, but have ability to "reset" a specific app's portion of this cache after uploading an updated .cfm or .cfc.

I also found one of Ahwin's blog posts pretty informative: http://blogs.sanmathi.org/ashwin/2006/07/12/tangling-with-the-template-cache/


Aug 29, 2009 at 10:19 PM // reply »
14 Comments

I must clarify that the Admin API does provide ability to clear individual templates from the cache.. What I meant, was that I want to be able to just update and upload any number of templates (such as via FTP), and then simply call the clearAppTrustedCache() function.


Sep 2, 2009 at 9:16 AM // reply »
10,640 Comments

@Aaron,

Ah, ok that makes sense. Seems like it would be a cool feature to have.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »