Thanks for asking!  Because now I can inherit from another CFC that does all
the caching for me, and I don't have to write specific code that accesses
server.  I can change the code from the way it is now to the new way by
simply inheriting from another CFC and adding one method call.  Also, if I
ever want to stop caching it, I can just stop inheriting the CFC that does
the caching and change init to call new()  (see below).  And is it really an
illusion?  As soon as I set instance to the old cached instance, my
reference to it is just as valid as the old one is.  So it really does
reside in the new plantSet object.  

I think this is pretty cool, because now I don't have to be aware of wether
or not plantSet is cached from anywhere outside of plantset, I can just call
createObject("component", 'plantSet') and get the cached version.  Before I
always had to call my objectCache cfc to get the cached version, and I had
to hope that all the other developers in my group always did it the same
way, too, and I had to write facades for access from flash.


Here's some sample code for my new PlantSet.cfc:


<cfcomponent name="PlantSet" displayname="Plant Set" hint="A directory of
plants." extends="CachedObject">

<cfset useCache()> <!--- this takes care of all my caching, and calls 'new'
only if neccessary --->

<cffunction name="init">
        <cfreturn this>
</cffunction>

<cffunction name="new" hint="This initializes the plant set.  I didn't want
to call it 'init', because I don't want this to run everytime">
        <!--- initialize all the plant structures and arrays --->
        ...
</cffunction>


The CachedObject.cfc has the useCache() method:

<cffunction name="useCache" access="public" returntype="void" output=0>
        <cfset var st = 0>
        <cfif cgi.script_name eq
'/CFIDE/componentutils/componentdetail.cfm'>
                <cfreturn> <!--- don't do anything if this is the component
browser --->
        </cfif>
        <cfset oVarCache = createObject("component",
"com.woodward.VarCache")>
        <cfset name=getMetaData(this).name>
        <cfif oVarCache.exists(name)>
                <cfset st = oVarCache.retrieve(name)>
        <cfelse>
                <cflock name=#name# type="EXCLUSIVE"
timeout="#keep.timeout#">
                        <cfif oVarCache.exists(name)>
                                <cfset st = oVarCache.retrieve(name)>
                        <cfelse>
                                <cfif isDefined("this.new")>
                                        <cfset new()>
                                </cfif>
                                <cfset st = structNew()>
                                <cfset st.self = self>
                                <cfset st.keep = keep>
                                <cfset oVarCache.put(name, st)>
                        </cfif>
                </cflock>
        </cfif>
        <cfset keep = st.keep>
        <cfset self = st.self>
</cffunction>

-----Original Message-----
From: Sean A Corfield [mailto:[EMAIL PROTECTED]
Sent: Friday, August 15, 2003 4:40 PM
To: [EMAIL PROTECTED]
Subject: Re: [CFCDev] CFC Persistance


OK, so in a design sense plantSet is *already* a facade to the object 
cache stored in server scope.

Why not have plantSet.getPlant("foo") simply do this:
        init();
        return server.plantCache[plantName];

And have init() work like this:
        if structKeyExists(server,"plantCache")
                return;
        // perform the long, expensive initialization
        // from database to Plant objects and store
        // them in the server scope plantCache

Since you're already doing (most of) that, I don't understand why you 
want the *illusion* that the data really resides inside your (otherwise 
stateless) plantSet object.

On Friday, Aug 15, 2003, at 14:58 US/Pacific, Brad Howerter wrote:

> I definitely do want to use the cached object data.  I'm not adding
> complexity and pain, I'm removing it.  Now I won't have to write 
> facades.
>
> Here's another attempt at explaining what I'm trying to do, and this 
> time
> I'll get more specific.
>
> I have a database table of plants.  I created a plantSet and a plant 
> cfc.
> The plantSet cfc, when init() is run, will create a plant object for 
> each
> row, and save them in arrays or structures under 'instance'.
>
> The plantSet object is stored in an objectCache cfc, and init() is only
> called the first time, before it is put in the server scope.  The
> objectCache cfc itself is stored in the server scope.
>
> Now, when I get plantSet back out of the objectCache cfc and
> plantSet.getPlant("Boston") is called, I don't have to requery the 
> database,
> I can just return the Boston plant from a structure in plantSet.
>
> This doesn't work, though, when Flash creates the plantSet object, 
> because
> it doesn't get the server one.  So I have to write a facade for Flash 
> that
> first gets the cached plantSet object, then calls getPlant.
>
> I believe I've followed the recommendations from you and Nathan so far.
>
> It'd be simpler to have plantSet run init() on itself the first time 
> it is
> created, and store itself in the server scope.  Then every time 
> plantSet is
> recreated, it will just retrieve its cached version of itself.  I can
> accomplish this by keeping data out of 'this' and just caching 
> 'instance'
> (actually I use keep and self, but let's say it's instance).
>
> Now, when flash calls plantSet.getPlant('Boston'), plantSet uses the 
> cached
> version of plantSet's data to return the Boston plant object.  In 
> effect,
> plantSet IS the cached version of itself because I've replaced all its 
> data
> with what was cached.  The only difference is that if I've made any 
> changes
> to the plantSet methods, those actually do get used instead of using 
> cached
> methods (since I'm not caching the methods, just the data).  I believe 
> that
> to be a benefit.
>
>
> -----Original Message-----
> From: Sean A Corfield [mailto:[EMAIL PROTECTED]
> Sent: Friday, August 15, 2003 3:04 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [CFCDev] CFC Persistance
>
>
> On Friday, Aug 15, 2003, at 11:35 US/Pacific, Brad Howerter wrote:
>> Actually, I don't need to do a deep copy.  I can just use 'instance'
>> instead
>> of the 'this' scope and then set this.instance = server.foo.instance.
>
> That just makes 'instance' refer to the data in the cached object. If
> you change data via that reference, you will be changing the cached
> object data which is probably not what you want.
>
> You probably want:
>       instance = duplicate(server.foo.instance);
> or:
>       instance = structCopy(server.foo.instance);
>
> But, again, you're now copying a lot of data which may be as expensive
> as the initialization!
>
> Then again, if you're *not* changing the instance data, why bother
> creating a new object instance in the first place?
>
> You still haven't explained what you are really trying to do - from the
> amount of pain and complexity you're going through, I can almost
> guarantee that you're going about this the wrong way...
>
> Sean A Corfield -- http://www.corfield.org/blog/
>
> "If you're not annoying somebody, you're not really alive."
> -- Margaret Atwood

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

Reply via email to