caching cfc's in structs and then calling them

6 views
Skip to first unread message

redtopia

unread,
Jun 7, 2010, 1:41:10 PM6/7/10
to CFCDev
I've got a struct containing other structs that contain components.
This is how I cache data type information for my application. Each
data type uses a CFC for data access, and don't want to keep having to
create a component each time I need to access data. So type
information and corresponding CFCs are cached.

Here's sample code that represents how one data type is cached:

<cfset dataTypes = StructNew ()>

<cfset dataTypes.article = StructNew ()>
<cfset dataTypes.article.name = "Article">
<cfset dataTypes.article.handler = createObject ("component",
"types.article").init ()>

Here's my question: What's the best way to access the handler?

I'm currently doing this (the variable "type" specifies the type of
data being accessed):

<cfif StructKeyExists (type)>
<typeInfo = StructFind (dataTypes, type)>
</cfif>

and then to get data:

<cfset data = typeInfo.handler.getData (id)>

I'm guessing that StructFind will return a copy of the struct, which
will cause the handler component to become duplicated in memory. If
that's true, how should I do this without duplicating the component in
memory?

Matt Quackenbush

unread,
Jun 7, 2010, 1:57:06 PM6/7/10
to cfc...@googlegroups.com
In ColdFusion, structs (and therefore CFCs) are passed by reference.  So, no, structFind() does not create or return a copy.  It returns a reference.

redtopia

unread,
Jun 7, 2010, 2:38:18 PM6/7/10
to CFCDev
Yes, I do realize that passing structs is done by reference. However,
the documentation for StructFind () says that it returns the value
associated with a key in a struct. It's not clear that it returns a
reference to the struct, or the value of that struct. I guess I can
write some code to verify.

redtopia

unread,
Jun 7, 2010, 2:50:35 PM6/7/10
to CFCDev
OK, I just wrote some code to verify, and you are correct.

Jared Rypka-Hauer

unread,
Jun 7, 2010, 5:04:26 PM6/7/10
to cfc...@googlegroups.com
Of course he's correct... he's Teh Kwak! ;)

J

Sean Corfield

unread,
Jun 7, 2010, 5:41:22 PM6/7/10
to cfc...@googlegroups.com
On Mon, Jun 7, 2010 at 10:41 AM, redtopia <jo...@redtopia.com> wrote:
> <cfif StructKeyExists (type)>
>   <typeInfo = StructFind (dataTypes, type)>
> </cfif>

Why not simply:

<cfif structKeyExists( dataTypes, type)>
<cfset typeInfo = dataTypes[ type ] />
</cfif>
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

redtopia

unread,
Jun 7, 2010, 6:23:49 PM6/7/10
to CFCDev
For some reason I don't use that syntax, probably because I come from
the C++ world. Do you think it's more efficient to access struct
members that way as opposed to using StructFind ()? I would assume
they would compile to an equiv. assembly. Structs are implemented as
hash tables, so CF probably does the same thing either way.

On Jun 7, 3:41 pm, Sean Corfield <seancorfi...@gmail.com> wrote:
> On Mon, Jun 7, 2010 at 10:41 AM, redtopia <j...@redtopia.com> wrote:
> > <cfif StructKeyExists (type)>
> >   <typeInfo = StructFind (dataTypes, type)>
> > </cfif>
>
> Why not simply:
>
> <cfif structKeyExists( dataTypes, type)>
>     <cfset typeInfo = dataTypes[ type ] />
> </cfif>
> --
> Sean A Corfield -- (904) 302-SEAN
> Railo Technologies, Inc. --http://getrailo.com/
> An Architect's View --http://corfield.org/

Tom Chiverton

unread,
Jun 9, 2010, 4:56:49 AM6/9/10
to cfc...@googlegroups.com
On Monday 07 Jun 2010 23:23:49 redtopia wrote:
> Do you think it's more efficient to access struct
> members that way as opposed to using StructFind ()? I would assume
> they would compile to an equiv. assembly. Structs are implemented as
> hash tables, so CF probably does the same thing either way.

Are you so close to the performance edge it matters ? If so, more RAM/CPU is
likely cheaper than your time :-)

--

Tom Chiverton
Developer

Tom.Ch...@halliwells.com
3 Hardman Square, Manchester, M3 3EB

****************************************************

This email is sent for and on behalf of Halliwells LLP.

Halliwells LLP is a limited liability partnership registered in England and Wales under registered number OC307980 whose registered office address is at Halliwells LLP, 3 Hardman Square, Spinningfields, Manchester, M3 3EB. A list of members is available for inspection at the registered office together with a list of those non members who are referred to as partners. We use the word “partner” to refer to a member of the LLP, or an employee or consultant with equivalent standing and qualifications. Regulated by the Solicitors Regulation Authority.

CONFIDENTIALITY

This email is intended only for the use of the addressee named above and may be confidential or legally privileged. If you are not the addressee you must not read it and must not use any information contained in nor copy it nor inform any person other than Halliwells LLP or the addressee of its existence or contents. If you have received this email in error please delete it and notify Halliwells LLP IT Department on 0870 365 2500.

For more information about Halliwells LLP visit
www.Halliwells.com.

redtopia

unread,
Jun 9, 2010, 10:54:52 AM6/9/10
to CFCDev
I wouldn't go back and rewrite everything, but it's good knowledge.

On Jun 9, 2:56 am, Tom Chiverton <tom.chiver...@halliwells.com> wrote:
> On Monday 07 Jun 2010 23:23:49 redtopia wrote:
>
> > Do you think it's more efficient to access struct
> > members that way as opposed to using StructFind ()? I would assume
> > they would compile to an equiv. assembly. Structs are implemented as
> > hash tables, so CF probably does the same thing either way.
>
> Are you so close to the performance edge it matters ? If so, more RAM/CPU is
> likely cheaper than your time :-)
>
> --
>
> Tom Chiverton
> Developer
>
> Tom.Chiver...@halliwells.com
> 3 Hardman Square, Manchester, M3 3EB
>
> ****************************************************
>
> This email is sent for and on behalf of Halliwells LLP.
>
> Halliwells LLP is a limited liability partnership registered in England and Wales under registered number OC307980 whose registered office address is at Halliwells LLP, 3 Hardman Square, Spinningfields, Manchester, M3 3EB.  A list of members is available for inspection at the registered office together with a list of those non members who are referred to as partners.  We use the word “partner” to refer to a member of the LLP, or an employee or consultant with equivalent standing and qualifications. Regulated by the Solicitors Regulation Authority.
>
> CONFIDENTIALITY
>
> This email is intended only for the use of the addressee named above and may be confidential or legally privileged.  If you are not the addressee you must not read it and must not use any information contained in nor copy it nor inform any person other than Halliwells LLP or the addressee of its existence or contents.  If you have received this email in error please delete it and notify Halliwells LLP IT Department on 0870 365 2500.
>
> For more information about Halliwells LLP visitwww.halliwells.com.
Reply all
Reply to author
Forward
0 new messages