Caching Objects Example
This example is taken from the Illidium Generator sample application I ported to ColdBox from Brian Rinaldi's project and you can find in the Bundle Download. Please support Brian's projects, he is a true contributor to the coldfusion community. This is an awesome generator.
The sample you will see below consists of three parts:
- Setup of an object in cache
- Testing of the object in cache
- Retrieveing the object in cache.
Remember that the ColdBox Object Cache Manager is available to you from any execution point. You only need to use the following snippet to have access to it:
<cfset var OCM = controller.getColdboxOCM()>
As for all the functionality of this wonderful caching system, please look at the caching guide and at the latest ColdBox CFC Api.
Step 1: Setup of an object in cache
The actual first setup of an object can occur in any point in time, but for this example I will do it in the onAppInit method of my application, which has been declared in my config.xml.cfm. This method executes on every application start.
<cffunction name="onAppInit" access="public" returntype="void" output="false"> <cfargument name="Event" type="coldbox.system.beans.requestContext"> <cfset var MyService = getSetting("AppMapping") & ".model.GeneratorService"> <cfset var GService = CreateObject("component",MyService).init(getSetting("adminpass"))> <!--- Place object in object cache and persist it with a default Framework Timeout. ---> <cfset getColdboxOCM().set("GService",GService)> </cffunction>
So what I do is set the MyService variable to the location of my Generator Service component first. Then I create the generator component and set it to the local variable GService. I then proceed to execute a set on the Object Cache Manager with the key and the object. I could have also provided a timeout for the object, but in this case, I want ColdBox to manage it for me. When you start your application and you look at the debugging panel. You will see the following snapshot:

From here you can tell that there is an Other Object in the cache, which will be the Generator Service.
Step 2: Testing if the object is in cache
Another important aspect of the ColdBox cache is that it will do garbage collection on them, so in order for me to use them, I have to verify that the objects are in memory. This is where the lookup kicks in. Look at the following code that I have place on my onRequestStart method. This means that the application tests on every request if the generator service is still in the cache and if its not, then it places it again. You could do this in a thousand different ways, but for purposes of example, this is the way I choose to do it.
<cffunction name="onRequestStart" access="public" returntype="void" output="false"> <cfargument name="Event" type="coldbox.system.beans.requestContext"> <!--- Check if the GService is set, else set it in cache ---> <cfif not getColdboxOCM().lookup("GService")> <cfset onAppInit(Event)> </cfif> </cffunction>
You can see that I first do a lookup of the GService key and it is not found, then I execute my AppInit method. Interesting note here, what do I do if the setup is in another handler and not the same one. Well, very easily I would call the event via the RunEvent("ehMyHandler.onAppInit") method. I do not pass the request context, because the framework takes care of it. This is a side note, but a very important one.
Step 3: Retrieving the object from cache
Now that I execute a normal event, I am assured that the generator service will still be in the cache. So I can go ahead and retrieve it with confidence:
<cffunction name="getDSNs" access="private" returntype="struct" output="false"> <cfreturn getColdboxOCM().get("GService").getModel("adminAPIService").getdatasources() /> </cffunction> //Another method below: <cffunction name="doGenerate" access="public" returntype="void" output="false"> <cfargument name="Event" type="coldbox.system.beans.requestContext"> <cfset var rc = Event.getCollection()> <cfset var gService = getColdboxOCM().get("GService")> <!--- EXIT HANDLERS: ---> <cfset rc.xehGenerate = "ehGeneral.doGenerate"> <!--- Get Setup ---> <cfset rc.DSNs = getDSNs() /> <cfset rc.dbType = getDBType()> <cfset rc.tables = getTables()> <!--- Get Table XML ---> <cfset gService.getModel(rc.dbType).setTable(Event.getValue("table")) /> <cfset gService.getModel(rc.dbType).setComponentPath(Event.getValue("componentPath")) /> <cfset rc.xmlTable = gService.getModel(rc.dbType).getTableXML() /> <!--- Get Generated CFC's ---> <cfset gService.getModel("xsl").configure(Event.getValue("dsn"),getSetting("xslBasePath")) /> <!--- get an array containing the generated code ---> <cfset rc.arrComponents = gService.getModel("xsl").getComponents(rc.xmlTable) /> <!--- Set the View ---> <cfset Event.setView("vwGeneration")> </cffunction>
As you can see from the samples above, I present two method examples. By using the getColdboxOCM().get("Key") method, I retrieve an object. Now what happens if the object is not there anymore. Then I will get an empty structure from the get method.
Well, I believe this pretty much covers how you can use the ColdBox Object Cache Manager to persist your objects and data in an intelligent and garbage collected fashion. Have fun and enjoy.
