Here's a quick and dirty way to do this, and it should work without too
much extra coding in CF, and relies on java garbage collection.

Lets' say you have an instance of your object called SINGLEOBJ.

Assuming originally you probably have <cfset
application.SINGLEOBJ=#someInstanceOfCFC#>

Then throughout your code you use various invocations of
application.SINGLEOBJ's methods.

Now we want to kill it off when no sessions are needing it.

Here's what I'd do... (this is untested, uncompiled pseudocode to
describe the idea). Make a java.lang.ref.WeakRerence and store that in
global application scope, so it "Doesn't count" as something keeping the
cfc around.  Use Strong references in sessions, since they get cleaned
up eventually.....

1) instead of Application.SINGLEOBJ being the CFC do this kind of thing:

<!--- singleton instantiation and insertion to global scope --->
<Cfif not isdefined("application.SINGLEOBJ")  or
application.singeobj.get() IS NULL>
<CFLOCK on application scope, exclusive..... >
<Cfinvoke YourComponent Construction here, name="MySingletonCFC">
<cfobject type="Java" action="Create" name="weak"
class="java.lang.ref.WeakReference">
<Cfset weak.init(MySingletonCFC)>
<Cfset Application.SINGLEOBJ=weak>
</CFLOCK>
</cfif>

2) For each session needing this object, at login time, or session
creation time, retreive it and store a (Strong) Reference in the
session.

<Cfset session.SINGLEOBJREF=application.singleobj.get()>

Now you have a copy of this in each session.  When there are sessions,
the CFC will stick around, since the garbage collector sees references
to it.  When the sessions get cleaned up, the only reference to the CFC
is a java WeakReference, which isn't cause for keeping it around, so the
garbage collector can mark it, queue it, and clean it up automatically.
You know when this happens when the weakreference (a variable you store
in the application) Returns null for get()...


Make sense?
-dov

------------------------

If the idea works correctly (attack it if you disagree) your CFC will
get disposed of during regular natural garbage collection


-----Original Message-----
From: Chris Jensen [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, December 21, 2004 8:40 PM
To: CF-Talk
Subject: Temporary Cache

Hi,
I need to cache a singleton type of component in the application scope
so that multiple sessions can make use of it. However the component
would be used rarely, and may take up significant resources, so I'd like
to remove it whenever it's not in use (ie when the last session thats
using it is destroyed).

ie, The seuqence of events would be something like this:

Two users come along, the user1 does something that uses my singleton
component, so I create it, and cache it in application scope.
User2 also starts using it while user1 is also using it.

User1 closes their browser, but user2 continues using the compionent for
a while.

Then user2 closes their browser, so noone is now using the component, so
remove it from the application scope.


I could write some Java classes to do this, but I'm guessing someone 
else has needed this before, so has it been written already?

Chris

-- 




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Special thanks to the CF Community Suite Gold Sponsor - CFHosting.net
http://www.cfhosting.net

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188521
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to